Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 825
David Nix Avatar asked Oct 19 '25 02:10

David Nix


2 Answers

Just write this code. Usually in viewDidLoad().

self.tabBarController.delegate = self;

If the current controller is a UITabBarController then:

self.delegate = self
like image 53
2 revs, 2 users 50%Waseem Avatar answered Oct 21 '25 15:10

2 revs, 2 users 50%Waseem


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.

like image 26
David Nix Avatar answered Oct 21 '25 16:10

David Nix



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!