在委托方法tabBarController shouldSelectViewController中检查所需的视图控制器:

时间:2013-12-03 07:23:12

标签: ios objective-c uitabbarcontroller

我在TabBarItems中有3 UITabBarController

<UINavigationController: 0xc76a680>
<SplitViewController: 0xc76a170>
<UINavigationController: 0xca5e6f0>

我在AppDelegate中有方法:

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
    NSLog(@"tab selected index %@",viewController);
    if (viewController == nil ) // I NEED TO IMPLEMENT A CHECk HERE
    {
        //show popup

        return NO; //does not change the tab
    }

    return YES; //does change the tab
}

那么如何检查应该选择的视图控制器是第二个导航控制器? THX

2 个答案:

答案 0 :(得分:6)

试试这段代码

 - (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
 {        
     BOOL result;

     if (viewController == [self.tabBarController.viewControllers objectAtIndex:2]) //assuming the index of uinavigationcontroller is 2
     {
         NSLog(@"Write your code based on condition");
         result = NO;
     } 
     else {
         result = YES;
     }

     return result;
  }

答案 1 :(得分:0)

您可以检查viewcontroller.title属性,然后做出决定。假设两个视图控制器的标题不同。