How do I access the data member variables of other view controllers?

I want to access the variables of a view controller data item in another view controller object.

Or is it possible to access my controls like the UILabel text property?

+2


source to share


4 answers


Many times when I find that I need to do this kind of thing, I find that I can reverse engineer the solution and the need goes away. Jay's Law: "If it's too hard, you're probably wrong."



+1


source


It is possible to access the UILabel of another view controller, but not needed. This will lead you to very difficult mistakes. Any IBOutlet can go null at amazing moments when memory is low. You cannot link directly to the UI elements of an object.

Your initial idea of ​​accessing data (model) objects is correct, although usually you'd be better off just initializing both view controllers with the same model object. For example, let's say you have a status message that you want to display in two different UILabels in two different view controllers. Rather than having one view controller requesting another view controller, it is better to have a model class such as Status in which both pointers have a pointer. Whenever it changes, they change their UILabel.



Your best bet is to post a notification (StatusDidChangeNotification) and just let anyone who needs it follow it and update their interface accordingly.

You want the UI elements to be very loosely coupled in Cocoa. Otherwise, you end up with hard errors when you do what appears to be a minor UI change.

+1


source


You will need to define the property in the view manager interface, and then if you have a reference to the view controller in the second view controller, you should be able to access it as text UILabel ..

0


source


viewWillAppear: only called by the framework when you are using built-in view controller controllers, such as presentModalViewController: animated: or pushViewController: animated :. In other cases, you need to call viewWill / Did (Dis) Appear: yourself.

0


source







All Articles