My app is using a tab bar controller with two tab bar items: Each item is a navigation controller.
I’m trying to access one view controller in one navigation controller from another view controller in the other navigation controller, is that possible?? and How??
I’d appreciate any possible help, Thanks.
Mohsen
You might consider using notifications or (even easier) Key-Value Observing.
I gave somebody some advice this morning about inter-controller communication in a UINavigationController context. Then this afternoon I tried KVO for the first time, and I had to go back and give different advice. KVO is WAY simpler than trying to chase things down in a view hierarchy. You just declare what’s observing what, and then set that thing up to catch change notifications. Piece of cake!
EDIT:
Notifications are probably the way to go here, actually–if you had both object instantiated in one place you could register one as an observer on the other’s keys, but that’s not necessarily the case if you’re building your tab bar from templates.
So, notifications. The idea is, you register one view controller as a notification observer, then fire notifications from another view controller, and the observer is notified when the notification is sent. It’s a little like your application sending email to itself.
Registering for and receiving the message looks like this:
ViewControllerOne.m:
And then sending the message is as simple as this:
ViewControllerTwo.m:
You are obviously not limited to passing a string in the
object:field. A somewhat more likely use would be to passself, and then you’d have access to any public fields of the notification-posting view controller.That’s the bare-bones usage of them. There’s a lot more subtleties you can get into, and it’s all laid out here:
http://developer.apple.com/iphone/library/documentation/cocoa/Conceptual/Notifications/Introduction/introNotifications.html