// // FourthViewController.h // UI1_UINavigationController // // Created by zhangxueming on 15/7/6. // Copyright (c) 2015年 zhangxueming. All rights reserved. // #import <UIKit/UIKit.h> @interface FourthViewController : UIViewController @end // // FourthViewController.m // UI1_UINavigationController // // Created by zhangxueming on 15/7/6. // Copyright (c) 2015年 zhangxueming. All rights reserved. // #import "FourthViewController.h" @interface FourthViewController () @end @implementation FourthViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor = [UIColor purpleColor]; //视图控制器数组 NSInteger count = [self.navigationController.viewControllers count]; NSLog(@"count = %li", count); UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeSystem]; btn1.frame = CGRectMake(100, 100, self.view.frame.size.width-200, 50); [btn1 setTitle:@"popToFirst" forState:UIControlStateNormal]; btn1.titleLabel.font = [UIFont boldSystemFontOfSize:24]; [btn1 addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside]; btn1.tag = 100; [self.view addSubview:btn1]; UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeSystem]; btn2.frame = CGRectMake(100, 200, self.view.frame.size.width-200, 50); [btn2 setTitle:@"popToSecond" forState:UIControlStateNormal]; btn2.titleLabel.font = [UIFont boldSystemFontOfSize:24]; [btn2 addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside]; btn2.tag = 101; [self.view addSubview:btn2]; } - (void)btnClicked:(UIButton *)btn { if (btn.tag==100) { //直接切换到根视图控制器 [self.navigationController popToRootViewControllerAnimated:YES]; } else if (btn.tag==101) { //获取指定位置的视图控制器 //视图控制器必须存在 UIViewController *controller = [self.navigationController.viewControllers objectAtIndex:1]; [self.navigationController popToViewController:controller animated:YES]; } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end
// // ThirdViewController.h // UI1_UINavigationController // // Created by zhangxueming on 15/7/6. // Copyright (c) 2015年 zhangxueming. All rights reserved. // #import <UIKit/UIKit.h> @interface ThirdViewController : UIViewController @end // // ThirdViewController.m // UI1_UINavigationController // // Created by zhangxueming on 15/7/6. // Copyright (c) 2015年 zhangxueming. All rights reserved. // #import "ThirdViewController.h" #import "FourthViewController.h" @interface ThirdViewController () @end @implementation ThirdViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor = [UIColor redColor]; UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem]; btn.frame = CGRectMake(100,200, self.view.frame.size.width-200, 50); [btn setTitle:@"pushToFourth" forState:UIControlStateNormal]; [btn addTarget:self action:@selector(btnClicked) forControlEvents:UIControlEventTouchUpInside]; btn.titleLabel.font = [UIFont boldSystemFontOfSize:24]; [self.view addSubview:btn]; NSLog(@"count = %li", self.navigationController.viewControllers.count); } - (void)btnClicked { FourthViewController *fvc = [[FourthViewController alloc] init]; [self.navigationController pushViewController:fvc animated:YES]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end
// // SecondViewController.h // UI1_UINavigationController // // Created by zhangxueming on 15/7/6. // Copyright (c) 2015年 zhangxueming. All rights reserved. // #import <UIKit/UIKit.h> @interface SecondViewController : UIViewController @end // // SecondViewController.m // UI1_UINavigationController // // Created by zhangxueming on 15/7/6. // Copyright (c) 2015年 zhangxueming. All rights reserved. // #import "SecondViewController.h" #import "ThirdViewController.h" @interface SecondViewController () @end @implementation SecondViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor = [UIColor yellowColor]; UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeSystem]; btn1.frame = CGRectMake(100, 100, self.view.frame.size.width-200, 50); [btn1 setTitle:@"pushToThird" forState:UIControlStateNormal]; btn1.titleLabel.font = [UIFont boldSystemFontOfSize:23]; [btn1 addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside]; btn1.tag = 100; [self.view addSubview:btn1]; UIButton *btn2= [UIButton buttonWithType:UIButtonTypeSystem]; btn2.frame = CGRectMake(100, 300, self.view.frame.size.width-200, 50); [btn2 setTitle:@"popToFirst" forState:UIControlStateNormal]; btn2.titleLabel.font = [UIFont boldSystemFontOfSize:24]; [btn2 addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside]; btn2.tag = 101; [self.view addSubview:btn2]; } - (void)btnClicked:(UIButton *)btn { //压栈 if (btn.tag == 100) { ThirdViewController *tvc = [[ThirdViewController alloc] init]; [self.navigationController pushViewController:tvc animated:YES]; } else if(btn.tag == 101) { //出栈 //栈顶的视图控制器出栈 [self.navigationController popViewControllerAnimated:YES]; } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end
// // AppDelegate.m // UI1_UINavigationController // // Created by zhangxueming on 15/7/6. // Copyright (c) 2015年 zhangxueming. All rights reserved. // #import "AppDelegate.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch //创建导航控制器 UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:self.window.rootViewController]; self.window.rootViewController = nav; return YES; }
// // ViewController.m // UI1_UINavigationController // // Created by zhangxueming on 15/7/6. // Copyright (c) 2015年 zhangxueming. All rights reserved. // #import "ViewController.h" #import "SecondViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.view.backgroundColor = [UIColor cyanColor]; UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem]; btn.frame = CGRectMake(100, 200, self.view.frame.size.width-200, 50); [btn setTitle:@"push" forState:UIControlStateNormal]; btn.titleLabel.font = [UIFont boldSystemFontOfSize:24]; [btn addTarget:self action:@selector(btnClicked) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn]; } - (void)btnClicked { SecondViewController *svc = [[SecondViewController alloc] init]; //取得导航控制器对象 //视图控制器必须被添加到导航控制器中 //把视图控制器压栈到导航控制器中 [self.navigationController pushViewController:svc animated:YES]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
时间: 2024-10-10 06:08:59