neobagram neobagram - 2 months ago 19
iOS Question

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) {

Thanks in advance for your help !


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