Reputation: 1419
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.
Upvotes: 8
Views: 13265
Reputation: 5645
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.
self.window = [[UIWindow alloc] init];
self.window.screen = [UIScreen mainScreen]; // <- The UITabViewController will not
// accept taps without this.
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.
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.
Upvotes: -1
Reputation: 4572
Use UITabBarControllerDelegate
instead of UITabBarDelegate
and
-tabBarController:didSelectViewController{}
instead of tabBar:didSelectItem{}
Interface
@interface TabBarController : UIViewController <UIApplicationDelegate, UITabBarControllerDelegate, UITabBarControllerDelegate>{
IBOutlet UITabBarController *tabBarController;
}
@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
@end
main file
@implementation TabBarController
@synthesize tabBarController;
/*other stuff*/
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
NSLog(@"rawr");
}
/*other stuff*/
@end
Upvotes: 1
Reputation: 684
-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item;
This method is a delegate method for UITabBar, not UITabBarController, so
self.tabBarController.delegate = self;
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:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
Upvotes: 16
Reputation: 11
You have synthesized the tab bar, so you now need to write: self.tabBarController.delegate = self;
Upvotes: 0
Reputation: 26385
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.
Upvotes: 0