简体   繁体   中英

Check the needed view controller in delegate method tabBarController shouldSelectViewController:

I have 3 TabBarItems in UITabBarController :

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

And I have the method in 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
}

So how to check that view controller which should be selected is the second Navigation Controller? Thx

try this code

 - (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;
  }

You could check for viewcontroller.title property and then make the decision. Assuming that title for both viewcontrollers are different.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM