我有一个包含 tabBarController、navigationController 和两个视图(xcode 4.2)的单视图应用程序。我想确定何时选择了给定的选项卡,并计划使用以下代码这样做:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSLog(@"in testtableView. %@", self.tabBarController.selectedViewController);
NSLog(@"in testtableView. tab bar index is: %i", self.tabBarController.selectedIndex);
}
此代码包含在两个视图中。当navigationController 不存在时,即当应用程序是一个简单的选项卡式应用程序时,它可以完美地工作。但是,当包含 navigationController 时,它会导致索引在第一次选择第二个选项卡(navigationController)时“滑动”一个,但如果用户决定继续在选项卡之间切换,则不会在此之后。
这是两个屏幕截图: 屏幕截图 http://s1305.photobucket.com/user/michael_jensen2/media/Screenshot2013-04-07at50743PM_zps1fab25ab.png.html?evt=email_share。我试着把它们放在 photobucket 上.. 有更好的方法吗?这是输出,在不同的测试中调用每个函数:
2013-04-07 17:21:31.728 testTabBar[1921:207] in firstViewController. <tabBarTestFirstViewController: 0x684ea80>
2013-04-07 17:21:53.626 testTabBar[1921:207] in secondViewConroller. <UINavigationController: 0x684fce0>
2013-04-07 17:22:04.528 testTabBar[1921:207] in firstViewController. <UINavigationController: 0x684fce0>
2013-04-07 17:22:06.544 testTabBar[1921:207] in secondViewConroller. <tabBarTestFirstViewController: 0x684ea80>
2013-04-07 17:22:07.392 testTabBar[1921:207] in firstViewController. <UINavigationController: 0x684fce0>
2013-04-07 17:22:07.816 testTabBar[1921:207] in secondViewConroller. <tabBarTestFirstViewController: 0x684ea80>
2013-04-07 17:22:08.288 testTabBar[1921:207] in firstViewController. <UINavigationController: 0x684fce0>
.
2013-04-07 17:50:33.075 testTabBar[2066:207] in firstViewController. tab bar index is: 0
2013-04-07 17:50:37.526 testTabBar[2066:207] in secondViewConroller. tab bar index is: 1
2013-04-07 17:50:37.982 testTabBar[2066:207] in firstViewController. tab bar index is: 1
2013-04-07 17:50:38.301 testTabBar[2066:207] in secondViewConroller. tab bar index is: 0
2013-04-07 17:50:38.668 testTabBar[2066:207] in firstViewController. tab bar index is: 1
2013-04-07 17:50:39.100 testTabBar[2066:207] in secondViewConroller. tab bar index is: 0
2013-04-07 17:50:39.532 testTabBar[2066:207] in firstViewController. tab bar index is: 1
2013-04-07 17:50:39.988 testTabBar[2066:207] in secondViewConroller. tab bar index is: 0
2013-04-07 17:50:40.372 testTabBar[2066:207] in firstViewController. tab bar index is: 1
有什么想法吗?可能已经发布了解决方案,但我还没有找到。
-麦克风