如果二级控制器也是包含tabbar
#import "AppDelegate.h"
#import "FirstVC.h"
#import "SecondVC.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
[self.window makeKeyAndVisible];
FirstVC *vc1 = [[FirstVC alloc] init];
vc1.view.backgroundColor = [UIColor magentaColor];
UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:vc1];
SecondVC *vc2 = [[SecondVC alloc] init];
vc2.view.backgroundColor = [UIColor cyanColor];
UINavigationController *nav2 = [[UINavigationController alloc] initWithRootViewController:vc2];
UITabBarController *tab = [[UITabBarController alloc] init];
self.window.rootViewController = tab;
tab.viewControllers = @[nav1, nav2];
nav1.tabBarItem.title = @"一级1";
nav2.tabBarItem.title = @"一级2";
return YES;
}
===============================================================================================================#import "FirstVC.h"
#import "ThirdVC.h"
@interface FirstVC ()
@end
@implementation FirstVC
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *nextPage = [UIButton buttonWithType:UIButtonTypeCustom];
[nextPage setTitle:@"next" forState:UIControlStateNormal];
[self.view addSubview:nextPage];
nextPage.frame = CGRectMake(0, 0, 50, 80);
nextPage.center = self.view.center;
[nextPage addTarget:self action:@selector(next) forControlEvents:UIControlEventTouchUpInside];
}
- (void)next {
//注意这里再创建新的控制器的时候不用再包一层nav了
ThirdVC *vc11 = [[ThirdVC alloc] init];
vc11.view.backgroundColor = [UIColor yellowColor];
UIViewController *vc12 = [[UIViewController alloc] init];
vc12.view.backgroundColor = [UIColor lightGrayColor];
UITabBarController *tab1 = [[UITabBarController alloc] init];
tab1.viewControllers = @[vc11, vc12];
vc11.tabBarItem.title = @"二级1";
vc12.tabBarItem.title = @"二级2";
self.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:tab1 animated:YES];
NSLog(@"FirstVC %@", self.navigationController);
self.hidesBottomBarWhenPushed = NO;
}
===============================================================================================================
#import "ThirdVC.h"
@interface ThirdVC ()
@end
@implementation ThirdVC
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *NEXT = [UIButton buttonWithType:UIButtonTypeSystem];
[NEXT setTitle:@"NEXT" forState:UIControlStateNormal];
[self.view addSubview:NEXT];
NEXT.frame = CGRectMake(0, 0, 50, 50);
NEXT.center = self.view.center;
[NEXT addTarget:self action:@selector(nEXT) forControlEvents:UIControlEventTouchUpInside];
}
- (void)nEXT {
UIViewController *vc111 = [[UIViewController alloc] init];
vc111.view.backgroundColor = [UIColor redColor];
//注意这里包裹这个控制器的tabVC和前一个页面属于同一级,所以要隐藏第一个tabbar,这句代码必须写
self.tabBarController.hidesBottomBarWhenPushed = YES;
//注意这里隐藏的是第二个tabbar
self.hidesBottomBarWhenPushed = YES;
//注意这里self.navigationController就是推过来tabVC的nav
[self.navigationController pushViewController:vc111 animated:YES];
NSLog(@"ThirdVC %@", self.navigationController);
self.hidesBottomBarWhenPushed = NO;
}