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:
-(void)viewDidLoad //a likely place, but not the only place you might do this
{
....
// whatever else you're doing to initialize your VC, and then
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(iWasNotified:)
name:@"myNotification"
object:nil];
}
-(void)iWasNotified:(NSNotification *)notification
{
NSString *passedValue = (NSString *)notification.object;
NSLog(@"We got notified and received: %@", passedValue);
}
And then sending the message is as simple as this:
ViewControllerTwo.m:
[[NSNotificationCenter defaultCenter]
postNotificationName:@"myNotification"
object:@"I'm passing you this NSString object!"];
You are obviously not limited to passing a string in the object:
field. A somewhat more likely use would be to pass self
, 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