In my header file I have this:
@interface TabBarController : UIViewController <UIApplicationDelegate, UITabBarDelegate, UITabBarControllerDelegate>{
IBOutlet UITabBarController *tabBarController;
}
-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item;
@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
@end
In my main file I have this:
@synthesize tabBarController;
-(void)viewDidLoad{
[super viewDidLoad];
self.tabBarController.delegate = self;
self.view = tabBarController.view;
}
-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
NSLog(@"rawr");
}
- (void)viewDidUnload {
[super viewDidUnload];
}
- (void)dealloc {
[tabBarController release];
[super dealloc];
}
@end
I have already connected my tabbarcontroller
as a delegate to my file's owner in interface builder, but it still never calls the didSelectItem
method.
Is there anything that I'm missing here?
I have already added tabBarController.delegate = self;
and it still does not work.
This method is a delegate method for UITabBar, not UITabBarController, so
will not work.
Tab bar controller has its own UITabBar, but changing the delegate of a tab bar managed by a tab bar controller is not allowed, so just try UITabBarControllerDelegate method like this:
You have synthesized the tab bar, so you now need to write:
self.tabBarController.delegate = self;
Just set the delegate for the tab bar. You can do it by setting
self.tabBarController.delegate = self;
or using Interface builder do to the tabbarcontroller object (not bar) see the connection inspector and drag the connection on the file's owner.There are many reasons, as indicated above, why it might not be working. Here is a more subtle reason. If you are creating your UI programatically (no storyboard or Xib) you need to set the screen of your UIWindow.
If you are using a Xib, I believe this is equivalent to having "Full Screen at Launch" selected. That has to be checked or I have noticed my Tabs don't work.
Update: Whatever, down-vote me if you must but I had a non-working tab controller and this is what I had to do to get it working. None of the other answers on this page helped me. I suppose using a xib/storyboard is pretty rare these days (I think this was from iOS 5 days) but leaving here for the person that does and stumbles into this situation.
You need to add this:
Use
UITabBarControllerDelegate
instead ofUITabBarDelegate
and-tabBarController:didSelectViewController{}
instead oftabBar:didSelectItem{}