先自定义一个UITabbarController,用于Storyboard中
再在MyTabbarController中实现protocol
@interface MyTabbarController : UITabBarController <UITabBarControllerDelegate> @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; }
时间: 2024-10-12 17:15:55