简体   繁体   中英

Accessing/passing common data between multiple ViewControllers

I am fairly new to developing in Swift, and I seem to be struggling with one of the key concepts.

I am looking to pass data between different UIViewControllers, allowing them all to access and manipulate it.

For example, in my application, I have created a simple data store class that contains and array of items. I want this array to be accessible by all ViewControllers.

I initialise the store within AppDelegate:

var itemStore = ItemStore()

I then create the first UIViewController and pass in the store so that it has access to it:

FirstViewController(itemStore: ItemStore)

So to do this I need to make changes to the init of FirstViewController so that it is able to accept itemStore as an argument.

I then want to pass that data to SecondViewController, and then to ThirdDataController.

It seems unnecessary to me that I have to edit every single UIViewController class so that it accepts the itemStore as an argument.

What are my options here? Some people have told me to store the data as a property of AppDelegate so that it is accessible by all. However, it seems that is not the right way of doing it.

Any advice?

You can pass data using a segue like this:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
    if (segue.identifier == “segueTest”) {

        var svc = segue.destinationViewController as secondViewController;

        svc.toPass = textField.text
    }   
}  

another solution

class AnsViewController: UIViewController {
   var theNum: Int

    override func viewDidLoad() {
        super.viewDidLoad()

        println(theNum)
    }

}

override func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
    let viewController = self.storyboard.instantiateViewControllerWithIdentifier("ansView") as AnsViewController
    viewController.num = theNum
    self.presentViewController(viewController, animated: true, completion: nil)
}

complete Demo Solution with Tutorial might be helpful to you.

Tutorial

edited Added NSUserDefault Solution to Save Data

let highscore = 1000
let userDefaults = NSUserDefaults.standardUserDefaults()
userDefaults.setValue(highscore, forKey: "highscore")
userDefaults.synchronize() // don't forget this!!!!

Then, when you want to get the best highscore the user made, you have to "read" the highscore from the dictionary like this:

if let highscore = userDefaults.valueForKey("highscore") {
    // do something here when a highscore exists
}
else {
    // no highscore exists
}
I hope this helps!

NSUserDefaults supports the following data types:

NSString NSNumber NSDate NSArray NSDictionary NSData

Here is the Complete Demo Code, might be helpful to you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM