Search code examples
swiftxcodenavigationsegue

Passing Data WITHOUT Navigation (Swift)


I'm looking pass data from one view controller to the next WITHOUT going to the next view controller. I don't know if this is possible, but if one of you know how, great. TO be more clear, I need to pass a variable from one screen to the next, but STAY on the first screen the entire time, while the next screen receives the data.

You may think mine is a duplicate, but I need to know how to pass data WITHOUT navigation. A lot of other ones include how to pass it, which includes navigation from one screen to the next.


Solution

  • There is a fantastical thing named NSUserDefaults. It is a dictionary that can be accessed at an time within the app. To set an object:

    NSUserDefaults.standardUserDefaults().setObject(YourObject, forKey: "key")
    

    Then to access it somewhere else:

    NSUserDefaults.standardUserDefaults().objectForKey("key")
    

    Keep in mind that you would have to cast the object in the second snippet, because the returned object is of type AnyObject.

    Hope this helps!