简体   繁体   中英

UITabBarController delegate methods never get called

I'm currently refactoring my iPad application and instead of a button on the NavigationBar, I would like to put my Logout button as a TabBar button item.

All of my views are in a unique StoryBoard so I get my TabBar in my Appdelegate.m by this way :

// Add logout tab to tabbar
storyboard                      = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UITabBarController *tabbar      = (UITabBarController *)[storyboard instantiateViewControllerWithIdentifier:@"tabbar"];
[tabbar setDelegate:self];

Of course the delegate protocol is declared in my Appdelegate.h

@interface AppDelegate : UIResponder <UIApplicationDelegate, UITabBarControllerDelegate>

After that, I create an empty viewController in order to create my logout tab :

UIViewController *logout        = [[UIViewController alloc]init];

Then I get tabbar viewControllers as NSMutableArray and add my logout VC :

NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:[tabbar viewControllers]];

[viewControllers addObject:logout];

Finally I set tabbar viewControllers with my new array :

[tabbar setViewControllers:viewControllers];

I think that those steps are correct. So why does the didSelectViewController is not called when I change the displayed tab ?

If it can help, here's my didSelectViewController method (which is write in AppDelegate.m)

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    NSLog(@"onglet sélectionné : %d / %d", [tabBarController selectedIndex], [[tabBarController viewControllers]count]);
    //select the index where your logout button is
    if ([tabBarController selectedIndex] == [[tabBarController viewControllers]count]-1) {
        NSLog(@"logout");
    }
}

Thanks in advance for your help !

Put [self setDelegate:self]; in your ViewDidLoad or somewhere where the object get's initialized

try intialising the tabbar object;

// Add logout tab to tabbar
storyboard                      = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UITabBarController *tabbar      = [[UITabBarController alloc] init];
tabbar      = (UITabBarController *)[storyboard instantiateViewControllerWithIdentifier:@"tabbar"];
[tabbar setDelegate:self];

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