1

Without using notifications, is the correct way to pass data from one view to another to use properties and assign the property to some UI element in viewDidLoad of the target view?

For example, if I have a single view app, there is a mainView, which appears first. I add another ViewController (ViewTwo), which has a label. If mainView tried to assign a string to this label in prepareForSegue, that would fail because the label is not initialized. Assigning first to a property on viewTwo and then to the UI element in viewDidLoad works.

Also, how do I get data from viewTwo back to mainView without using notifications?

Lebyrt
  • 1,376
  • 1
  • 9
  • 18
4thSpace
  • 43,672
  • 97
  • 296
  • 475

1 Answers1

1

This might help:

How to pass prepareForSegue: an object

If you pass an object to the second view, the first view can also retain ownership and read what the second view wrote after the second view is gone.

To clarify (and this code is from above link), the idea is in prepareForSegue you get the destination viewcontroller as an object to work with. For example:

// Get reference to the destination view controller
YourViewController *vc = [segue destinationViewController];

What's going on is the destination view controller has been instantiated. But you are in pepareForSegue getting the second view controller before it appears. In fact viewDidLoad would be called after prepareForSegue. Here is an interesting link on that: viewDidLoad called before prepareForSegue finishes (just so the title of the link doesn't mislead, the author reconciled that it was not calling viewDidLoad before prepareForSegue)

Since viewDidLoad is not yet called, but the view is initialized, a programmer can for example populate NSString's that are instance variables on the second view and in the viewDidLoad method of the second view it can use those strings to set it's labels. This is a good reference for that Passing Data between View Controllers

Delegates and Protocols are discussed in this link also which are an interface by which one view can call a function with arguments(part of a protocol the view defines) on the other view (which implements the protocol).

Properties set can be an object of a class and be objects that are shared as properties on both viewA and viewB such as an Array or object of a custom class where the property on viewA and ViewB can be made to point to the same thing and after ViewB is gone viewA can still use the data. This also let's you keep the state of viewB persistent such as if there is a game on viewB you could reconstruct the place they were at in the game by the data you keep persistent as properties of viewA or a chat window could be made to open up on viewB where they left off reading.

Specifically addressing correctness (that is a loaded word) this is a proper way to do things but one implication of setting properties is you have to think about memory. An object that has multiple references and retains references in ViewA after ViewB is gone, takes up memory. If viewA owned everything on viewB for example it's possible to hold onto subviews of viewB etc, the memory overhead is higher. If viewA was holding onto game data while a game was ongoing as they moved between viewA and viewB but did not need it after the game was over it should be set to nil to free memory.

Community
  • 1
  • 1
LanternMike
  • 664
  • 1
  • 5
  • 16