0

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.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • Possible duplicate of [Passing Data between View Controllers](http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – JAL Apr 23 '16 at 22:08
  • I believe that one involves navigating. Anyway to avoid navigation between screens, but still pass the data? @JAL – Andy Lebowitz Apr 23 '16 at 22:09
  • You're telling me you went through every single answer in the linked question and still think it's not a duplicate? I think not. Read up on object delegation and protocols. – JAL Apr 23 '16 at 22:11
  • It's not a duplicate. And yes, I have gone through MANY tutorials and questions in the past few hours. All the same. @JAL – Andy Lebowitz Apr 23 '16 at 22:11
  • 1
    Can't you just save the data somewhere, and then pass it when you'll eventually navigate to the next view controller? – Tim Vermeulen Apr 23 '16 at 22:14
  • @TimVermeulen Does NSUserDefaults save it so you can access it from any view controller? Just wondering – Andy Lebowitz Apr 23 '16 at 22:21
  • For example, store your data in some array or dictionary property of initial view controller (don't know which is suitable for your case), then, when time comes to show next view controller, pass all of this data via `prepareForSegue`. – Alexander Doloz Apr 23 '16 at 22:26
  • Please don't change your question. If you have a different question, please [ask a new question](http://stackoverflow.com/questions/ask). – JAL Apr 24 '16 at 03:24

2 Answers2

0

Yes, it's possible. But you'll have to instantiate your next view controller and keep a reference to it somewhere. Also this view controller must be visible to your initial view controller.

Anyway, maybe you should change architecture of your app to avoid this strange passing of data.

Alexander Doloz
  • 4,078
  • 1
  • 20
  • 36
0

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!

DanielEdrisian
  • 716
  • 1
  • 4
  • 17