Waffles
Waffles

Reputation: 121

How would I have one view controller access a variable from another view controller?

Everything I've seen on stack is passing the data from an input, onto another view controller on a button press. Let's say I have var banana that is an array of dictionaries, but once my function in ViewA.swift is done loading up banana, I want another viewController, say ViewB.swift to manipulate that data as it sees fit. I do NOT have a segue going from one view controller to the other. EDIT: It's actually two TableViewControllers****

I've looked into NSNotificationCenter, but that doesn't seem to work with my variable type, which is an array of dictionaries

Upvotes: 0

Views: 515

Answers (3)

D. Degroot
D. Degroot

Reputation: 11

Make sure that you have given all ViewControllers an identifier, then instantiate them with:

guard let viewControllerB = storyboard?.instantiateViewControllerWithIdentifier("ViewControllerB") as? ViewControllerB else {
    fatalError(); return
}
// then access the variable/property of ViewControllerB
viewControllerB.banana = whatEver

Added for clarification

This one works for me.

Just make sure that you have given the TableViewController an identifier otherwise you will not be able to instantiate it. Also make sure that you cast the result of instantiateViewControllerWithIdentifier to your TableViewController class otherwise you won't be able to access it's variables (I've seen that you were struggling with this; if you get an error that UIViewController doesn't have a member "myArray" then you probably have forgotten to cast the result)

class TableViewController: UITableViewController {
    var myArray = [String]()
}

class ViewController: UIViewController {
    func someEventWillTriggerThisFunction() {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        guard let tableViewController = storyboard.instantiateViewControllerWithIdentifier("TableViewController") as? TableViewController else {
            fatalError(); return
        }
        tableViewController.myArray = ["Value1", "Value2", "Value3"]
        /* if you want to present the ViewController use this: */
        self.presentViewController(tableViewController, animated: true, completion: nil)
    }
}

Upvotes: 0

Soumya Ranjan
Soumya Ranjan

Reputation: 4843

Use NSNotificationCenter for accessing data.

Try Below code

//Sent notification 

 let dictionary = ["key":"value"]    
 NSNotificationCenter.defaultCenter().postNotificationName("passData", object: nil, userInfo: dictionary)

//Receive  notification 

 NSNotificationCenter.defaultCenter().addObserver(self, 
 selector:"myMethod:", name: "passData", object: nil)

//Receive  notification method

 func myMethod(notification: NSNotification){
 print("data: \(notification.userInfo!["key"])")

Upvotes: 1

Ulysses
Ulysses

Reputation: 2317

Without using segue, you can instantiate the View controller, and set the public parameteres.

let storyboard = UIStoryboard(name: "MyStoryboardName", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("someViewController") as! ViewB

/* Here you have the reference to the view, so you can set the parameters*/

vc.parameterInViewB = banana

/* At this point you can present the view to the user.*/

self.presentViewController(vc, animated: true, completion: nil)

Upvotes: 0

Related Questions