uinavigationController抛出self.tabBarController.selectedViewController

时间:2013-04-07 21:58:07

标签: uinavigationcontroller uitabbarcontroller

我有一个包含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)时“滑动”一个,但如果用户决定继续在选项卡之间切换,则不会出现这种情况。

以下是两个截屏: screen shots 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

任何想法?可能有一个已经发布的解决方案,但我还没有找到它。

-Mike

1 个答案:

答案 0 :(得分:0)

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController;
{

    if (tabBarController.selectedIndex == 0) {




    }
    else if (tabBarController.selectedIndex == 1)
    {



    }

}