0

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?

Fabrizio Prosperi
  • 1,398
  • 4
  • 18
  • 32

1 Answers1

0

I can't claim to know what you are trying to accomplish by sending data from one viewcontroller to another, but if sending data is your only purpose then I would recommend the Singleton Pattern. Or I would recommend NSNotificationCenter. A singleton could hold app wide data that can be retrieved from anywhere. You could even (though its against best practices) hold references to your viewcontrollers in the singleton class. I also heavily use notifications to pass around data. Simply post a notification and have another class listen for it.

For instance in firstViewController in viewDidLoad you could do this:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(theMethodToCall:) name:@"myNotificationName" object:nil];

Then in secondViewController, when you want to pass data you could post this( Where "myData" is an data object (Like NSDictionary)):

[[NSNotificationCenter defaultCenter] postNotificationName:myNotificationName object:nil userInfo: myData];

Then in firstViewController this method would receive the data.

-(void)theMethodToCall:(NSNotification *)notif
 {
      NSDictionary *dict = [notification userInfo];
 }

With this code you also get the nice object reference

spentak
  • 4,627
  • 15
  • 62
  • 90
  • Hi Spentak, of course you are right and I am indeed already using the singleton pattern to pass global data to different view controllers. In this case I avoided it since the receiving view controller is basically editing data of the row selected in the sending view controller. This was somehow inherited from the way the application was before I introduced the tab bar controller,but I will explore the notification way even if it does not seem as clean. Nevertheless my question was to understand whether there is a better way than mine to identify the different tab bar view controller instances. – Fabrizio Prosperi Aug 21 '11 at 23:25