tabBar didSelectItem似乎没有工作

在我的头文件中,我有这样的:

@interface TabBarController : UIViewController <UIApplicationDelegate, UITabBarDelegate, UITabBarControllerDelegate>{ IBOutlet UITabBarController *tabBarController; } -(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item; @property (nonatomic, retain) IBOutlet UITabBarController *tabBarController; @end 

在我的主文件中,我有这样的:

 @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 

我已经把我的tabbarcontroller作为一个委托连接到了我的文件的所有者,但它仍然不会调用didSelectItem方法。

有什么我在这里失踪?

我已经添加tabBarController.delegate = self; 它仍然不起作用。

 -(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item; 

这个方法是UITabBar的委托方法,而不是UITabBarController

 self.tabBarController.delegate = self; 

不pipe用。

标签栏控制器有它自己的UITabBar,但不允许更改由标签栏控制器pipe理的标签栏的委托,所以只需尝试像这样的UITabBarControllerDelegate方法:

 - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController 

你需要添加这个:

 tabbarcontroller.delegate = self; 

使用UITabBarControllerDelegate而不是UITabBarDelegate
-tabBarController:didSelectViewController{}而不是tabBar:didSelectItem{}

接口

 @interface TabBarController : UIViewController <UIApplicationDelegate, UITabBarControllerDelegate, UITabBarControllerDelegate>{ IBOutlet UITabBarController *tabBarController; } @property (nonatomic, retain) IBOutlet UITabBarController *tabBarController; @end 

主文件

 @implementation TabBarController @synthesize tabBarController; /*other stuff*/ - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{ NSLog(@"rawr"); } /*other stuff*/ @end 

只需设置标签栏的委托。 你可以通过设置self.tabBarController.delegate = self; 或者使用Interface builder做tabbarcontroller对象(不是栏),看连接检查器,并拖动文件所有者的连接。

你已经合成了标签栏,所以你现在需要写: self.tabBarController.delegate = self;

如上所述,有很多原因,为什么它可能不工作。 这是一个更微妙的原因。 如果以编程方式创buildUI(无故事板或Xib),则需要设置UIWindow的屏幕。

 self.window = [[UIWindow alloc] init]; self.window.screen = [UIScreen mainScreen]; // <- The UITabViewController will not // accept taps without this. 

如果您使用的是Xib,我相信这相当于select了“全屏启动”。 必须检查,或者我注意到我的标签不起作用。

更新:无论如何,如果你必须投我票,但我有一个非工作选项卡控制器,这是我必须做的,以使其工作。 没有在这个页面上的其他答案帮助我。 我想现在使用xib / storyboard是非常罕见的(我认为这是iOS 5天),但是离开这里是为了这个人而做的。