5

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 !

neobagram
  • 87
  • 1
  • 8

2 Answers2

9

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

Aamir
  • 13,234
  • 9
  • 51
  • 63
kagmanoj
  • 4,262
  • 5
  • 17
  • 21
  • You mean set the delegate in an other viewController ? EDIT I ask this because currently all the code you see above in my post is in Appdelegate.m and this file does not have viewDidLoad method – neobagram Feb 04 '14 at 10:48
  • It works ! I set the delegate in an other viewController and the method gets called – neobagram Feb 04 '14 at 10:54
-2

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];
gaurish.salunke
  • 919
  • 8
  • 14