![](/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.