tabBar didSelectItem seems not to be working
Solution 1
-(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
Solution 2
You need to add this:
tabbarcontroller.delegate = self;
Solution 3
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
Rowie Po
Updated on June 15, 2022Comments
-
Rowie Po about 2 years
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 thedidSelectItem
method.Is there anything that I'm missing here?
I have already added
tabBarController.delegate = self;
and it still does not work.