mobibob
mobibob

Reputation: 8794

My UITabBarController's didSelectViewController method is not getting called?

Here is my code stub for my app-delegate.m -- it never gets called.

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    NSLog(@"%s", __FUNCTION__);
}

It is defined in this app-delegate.h

@interface OrioleAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {
    UIWindow *window;
    UITabBarController *tabBarController;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;

@end

Upvotes: 3

Views: 9948

Answers (3)

CedricSoubrie
CedricSoubrie

Reputation: 6697

If your ViewController is a UITabBarController, you need to set self as it's delegate because you can't change the delegate of the UITabBar directly.

For example, in the ViewDidLoad of your UITabBarController :

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.delegate = self;
}

Upvotes: 15

Shaggy Frog
Shaggy Frog

Reputation: 27601

Did you make a connection between your UITabBarController and your application delegate?

- (void)applicationDidFinishLaunching:(UIApplication *)application
{
     ...
     tabBarController.delegate = self;
     ...
}

Upvotes: 15

mobibob
mobibob

Reputation: 8794

I added the following tabBarController.delegate = self; and all is well. I hope this is helpful to others.

- (void)applicationDidFinishLaunching:(UIApplication *)application {

    // Add the tab bar controller's current view as a subview of the window
    tabBarController.delegate = self;
    [window addSubview:tabBarController.view];
}

Upvotes: 0

Related Questions