开发者

Identify UITabBarItems view controller instances the proper way

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] viewCont开发者_如何学Pythonrollers] 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?


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜