scenedelegate.m
#import "SceneDelegate.h" #import "VCRoot.h" @interface SceneDelegate () @end @implementation SceneDelegate - (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions { self.window = [[UIWindow alloc] initWithWindowScene:(UIWindowScene *)scene]; self.window.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height); self.window.rootViewController=[[UINavigationController alloc]initWithRootViewController:[[VCRoot alloc]init]]; [self.window makeKeyAndVisible]; }
VCRoot.m
#import "VCRoot.h" #import "VCSecond.h" @interface VCRoot () @end @implementation VCRoot - (void)viewDidLoad { [super viewDidLoad]; //设置导航栏透明度 self.navigationController.navigationBar.translucent=NO; self.navigationController.navigationBar.barStyle=UIBarStyleDefault; self.title=@"根视图"; UIBarButtonItem* next = [[UIBarButtonItem alloc] initWithTitle:@"下一页" style:UIBarButtonItemStyleDone target:self action:@selector(preeNext)]; self.navigationItem.rightBarButtonItem=next; } -(void)preeNext { //创建一个新的导航控制器 VCSecond* vcSecond= [[VCSecond alloc]init]; //使用当前视图控制器的导航对象 [self.navigationController pushViewController:vcSecond animated:YES]; } @end
VCSecond.m
#import "VCSecond.h" #import "VCThird.h" @interface VCSecond () @end @implementation VCSecond - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor=[UIColor greenColor]; self.title=@"视图二"; UIBarButtonItem* btnNext=[[UIBarButtonItem alloc]initWithTitle:@"第三级" style:UIBarButtonItemStyleDone target:self action:@selector(pressNext)]; self.navigationItem.rightBarButtonItem=btnNext; } -(void)pressNext { VCThird* vcThired=[[VCThird alloc]init]; [self.navigationController pushViewController:vcThired animated:YES]; }
VCThird.m
#import "VCThird.h" @interface VCThird () @end @implementation VCThird - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor=[UIColor redColor]; self.title=@"视图三"; UIBarButtonItem* btnleft = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:self action:@selector(pressroot)]; self.navigationItem.leftBarButtonItem=btnleft; } -(void)pressroot { [self.navigationController popToRootViewControllerAnimated:YES]; } -(void)pressback { //将当前视图控制器弹出返回上一级界面 [self.navigationController popViewControllerAnimated:YES]; }