![](/img/trans.png)
[英]tabBar:didSelectItem and popToRootViewControllerAnimated
[英]tabBar didSelectItem seems not to be working
在我的頭文件中,我有這個:
@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;
不管用。
標簽欄控制器有自己的UITabBar,但是不允許更改由標簽欄控制器管理的標簽欄的委托,所以只需嘗試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;
只需設置標簽欄的代理。 你可以通過設置self.tabBarController.delegate = self;
或者使用“接口”構建器對tabbarcontroller對象(而不是條形)執行操作,請參閱連接檢查器並在文件所有者上拖動連接。
如上所述,有許多原因導致它可能無法正常工作。 這是一個更微妙的原因。 如果以編程方式創建UI(無故事板或Xib),則需要設置UIWindow的屏幕。
self.window = [[UIWindow alloc] init];
self.window.screen = [UIScreen mainScreen]; // <- The UITabViewController will not
// accept taps without this.
如果您使用的是Xib,我相信這相當於選擇了“啟動時全屏”。 必須檢查或我注意到我的標簽不起作用。
更新:無論如何,如果必須,請向下投票,但我有一個不工作的選項卡控制器,這是我必須要做的才能讓它工作。 這個頁面上的其他答案都沒有幫助我。 我想使用xib / storyboard這些天很少見(我認為這是來自iOS 5天)但是離開這里的人會遇到並偶然發現這種情況。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.