Reputation: 3334
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?
Upvotes: 4
Views: 6352
Reputation: 149
Just write this code. Usually in viewDidLoad()
.
self.tabBarController.delegate = self;
If the current controller is a UITabBarController
then:
self.delegate = self
Upvotes: 11
Reputation: 3334
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.
Upvotes: 3