I have an application with a Tab Bar to open different view controllers like this:
firstViewController = [[UITableViewController alloc] init];
UINavigationController *firstNavigationController = [[UINavigationController alloc] initWithRootViewController:firstViewController];
[firstNavigationController setTabBarItem:[[UITabBarItem alloc] initWithTitle:@"Add" image:[UIImage imageNamed:@"Add.png"] tag:1]];
[viewControllers addObject:firstNavigationController];
secondViewController = [[UITableViewController alloc] init];
UINavigationController *secondNavigationController = [[UINavigationController alloc] initWithRootViewController:secondViewController];
[secondNavigationController setTabBarItem:[[UITabBarItem alloc] initWithTitle:@"List" image:[UIImage imageNamed:@"List.png"] tag:2]];
[viewControllers addObject:secondNavigationController];
UITabBarController *tabBarController = [[UITabBarController alloc] init];
[tabBarController setViewControllers:viewControllers];
[[self window] setRootViewController:tabBarController];
This is fine. Now I have an additional navigation requirement, the firstViewController might be called from the secondViewController, passing data to it.
The only way I have found to pass data to the very same instance of firstViewController which is accessed by the tab bar is the following (in the secondViewController code):
firstViewController = [[[[[self tabBarController] viewControllers] objectAtIndex:0] viewControllers] objectAtIndex:0];
This works fine, but I find it messy, particularly if I decide to change the order of the views in the tab bar controller.
I have also explored the tag way, but didn't seem to improve the code that much.
Is there another, cleaner, way?