【发布时间】:2011-08-19 19:44:15
【问题描述】:
我有一个带有标签栏的应用程序,可以像这样打开不同的视图控制器:
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];
这很好。现在我有一个额外的导航需求,firstViewController 可能会从 secondViewController 中调用,并将数据传递给它。
我发现将数据传递给标签栏访问的同一个 firstViewController 实例的唯一方法如下(在 secondViewController 代码中):
firstViewController = [[[[[self tabBarController] viewControllers] objectAtIndex:0] viewControllers] objectAtIndex:0];
这很好用,但我觉得它很乱,尤其是当我决定更改标签栏控制器中视图的顺序时。
我也探索过标记方式,但似乎对代码没有太大的改进。
还有其他更清洁的方法吗?
【问题讨论】:
标签: iphone uiviewcontroller uitabbarcontroller uitableview uitabbaritem