先自定義一個UITabbarController,用于Storyboard中
再在MyTabbarController中實現protocol
@interface MyTabbarController : UITabBarController
@end再實現代理里面的方法
@implementation MyTabbarController
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
NSLog(@"shouldSelectViewController %@", tabBarController.selectedViewController);
if (viewController.tabBarItem.tag == 100) {
DiaryViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:DIARY_VC_ID];
[((UINavigationController *)tabBarController.selectedViewController) pushViewController:vc animated:YES];
return NO;
}
return YES;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
self.delegate = self;
}
return self;
}
@end在要監聽的tabbaritem跳轉的viewcontroller中(比如點擊一個item隱藏tabbar,而且有返回按鈕)
則找到該tabbar,我的是父控件的tabbar,所以
- (void)viewWillAppear:(BOOL)animated {
self.parentViewController.tabBarController.tabBar.hidden = YES;
}點擊返回按鈕后回到開始所選中的tabbaritem
- (void)viewWillDisappear:(BOOL)animated {
self.parentViewController.tabBarController.tabBar.hidden = NO;
}
原文:http://blog.csdn.net/fanxiaoxuan1234/article/details/41721635