Reputation: 16029
I want to access the value of a view controller data member variables in another view controller object.
Or is it possible to access its controls like UILabel text property?
Upvotes: 2
Views: 509
Reputation: 169
viewWillAppear: is only called by the framework when you use the built-in view controller transitions like presentModalViewController:animated: or pushViewController:animated:. In other cases, you have to call viewWill/Did(Dis)Appear: yourself.
Upvotes: 0
Reputation: 14471
A lot of times when I find I have to do things like that I find I can redesign the solution and the need for it goes away. Jay's law: "If it's too hard you're probably doing it wrong."
Upvotes: 1
Reputation: 299345
It is possible to access a UILabel of another view controller, but don't. It will lead you to very hard-to-understand bugs. Any IBOutlet can become nil at surprising times when memory is low. You shouldn't mess with another object's UI elements directly.
Your initial idea of accessing the data (model) objects, is the right one, though generally you will be better off to just initialize both view controllers with the same model object. For instance, say you have a status message that you want to show up in two different UILabels in two different view controllers. Rather than have one view controller ask the other view controller for the data, it's better to have a model class like "Status" that both views have a pointer to. Whenever it changes, they change their UILabel.
Even better is to post a notification (StatusDidChangeNotification) and just let everyone who cares observe it and update their UI appropriately.
You want to keep UI elements very loosely coupled in Cocoa. Otherwise you wind up with hard-to-fix bugs when you make what seems like a minor UI change.
Upvotes: 1
Reputation: 22395
You are going to have to define the property in the view controllers interface, then as long as you have a reference to the view controller in the second view controller you should be able to access it like the text of a UILabel..
Upvotes: 0