tabBar didSelectItem seems not to be working

房东的猫 提交于 2019-12-08 18:46:33

问题


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.


回答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



回答2:


You need to add this:

tabbarcontroller.delegate = self;



回答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



回答4:


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.




回答5:


You have synthesized the tab bar, so you now need to write: self.tabBarController.delegate = self;




回答6:


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.

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.



来源:https://stackoverflow.com/questions/10946058/tabbar-didselectitem-seems-not-to-be-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!