Search code examples
iphoneioscocoa-touchdelegatesuitabbarcontroller

tabBarController:shouldSelectViewController: not being called when delegate is set


I think I've done my homework here.

I want my app delegate to be the delegate for my UITabBarController.

Using IB, I've connected UITabBarController's delegate to my Application Delegate.

App Delegate Header file is:

@interface MyAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {

The delegate method I'm trying to implement is:

-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    NSLog(@"shouldSelectViewController called.");
    // do some stuff with viewController
    return YES;
}

My app delegate has an outlet to the UITabBarController that's connected in IB. When I do this:

NSLog(@"tab bar controller delegate is %@", self.tabBarController.delegate);

I get a good result such as tab bar controller delegate is <MyAppDelegate: 0x6e86a30>.

What am I missing?


Solution

  • Ok, found the solution. I had some old code in my RootViewController that set up this controller as the delegate. No delegate methods were implemented on the RootViewController, so it appeared as if nothing was happening. Because the RootViewController is set as delegate AFTER MyAppDelegate, the delegate was actually set to the RootViewController.

    So the lesson is double-check your code to make sure some other object isn't also being set as the delegate.