Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tabBar didSelectItem seems not to be working

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.

like image 896
Rowie Po Avatar asked Jun 08 '12 09:06

Rowie Po


3 Answers

-(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
like image 179
idearibosome Avatar answered Oct 12 '22 00:10

idearibosome


You need to add this:

tabbarcontroller.delegate = self;
like image 33
iOSDev Avatar answered Oct 12 '22 02:10

iOSDev


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
like image 22
Sameera R. Avatar answered Oct 12 '22 00:10

Sameera R.