0

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?

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

10 Comments

So that sets a property of the next ViewController to the data that you are passing. However, it seems strange to me that you have to pass it again and again and again (if you go from AViewController to BViewController to CViewController). Is there no way of accessing it globally?
@BrianMarsh yes just declare it at top level. If you want it available only at those two view controllers, put them both at the same swift file and declare you var private at this file at top level
@BrianMarsh, You can also Save it on UserDefault. or Local DataBase or in .Plist File.. there are many ways. here i have given you the simplest way to implement it.
@LeoDabus - Sorry, what do you mean when you say declare it at top level?
@BrianMarsh outside your class after import Statements
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.