문제

Probably a simple question, but I'm having a hellofa time finding a solution to it.

I need to find the identifier of the current tab in a tabbarcontroller and use it in a conditional to run a method.

how do I find this?

if (self.tabbarcontroller.identifier == @"My identifier") {
   // do some method
} else {
   // do the default method
}
도움이 되었습니까?

해결책

UIWindow *window = [[UIApplication sharedApplication] keyWindow];
UITabBarController *tabBarController = (UITabBarController*) window.rootViewController;
UIViewController *selectedVC = tabBarController.selectedViewController;
if ([selectedVC.identifier isEqualToString:@"anIdentifier"])
{
  // Do something
} else {
  // Do something else
}

You can set the identifier of a ViewController in your storyboard

다른 팁

Check out the following code. Also make sure that the delegate for the UITabBar is correct pointing to the view controller, in this case FirstViewController.

**FirstViewController.h****

@interface FirstViewController : UIViewController<UITabBarDelegate>

**FirstViewController.m:**

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    NSLog(@"%@",[item tag]);
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top