AppDelegate.m
#import "AppDelegate.h"
#import "RootViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
/*——————————————————————————————————————————————————————————————————————————————-*/
RootViewController *root = [[RootViewController alloc]init];
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:root];
self.window.rootViewController = nav;
//screen -> window -> NavigaitonC -> ViewC -> view (subviews)
/**
* UINavigationController 導航控制器
1.導航控制器有且只有一個根視圖控制器
2.PUSH 壓入新視圖控制器 該VC計數+1 會被add進入 [導航控制器]的ViewControllers數組
3.POP 彈出視圖控制器 該VC計數-1 從viewControllers中remove
4.導航控制器管理視圖控制器 采用棧結構 先入后出的原則 先push進來的vc一定后pop
*/
return YES;
}
@end
RootViewController.m
#import "RootViewController.h"
@interface RootViewController ()
@property (weak, nonatomic) IBOutlet UITextField *field;
@end
@implementation RootViewController
//Push的方法
- (IBAction)push:(UIButton *)sender {
//1.PUSH 壓入 一個試圖控制器 (新控制器蓋住舊控制器)
RootViewController *vc = [[RootViewController alloc]init];
vc.view.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1];
//PS:PUSH是導航控制器的功能,所以要用導航控制器調用
[self.navigationController pushViewController:vc animated:YES];
}
- (IBAction)pop:(UIButton *)sender {
//2.彈出
//返回到指定控制器
// [self.navigationController popToViewController:(nonnull UIViewController *) animated:YES];
//返回上一級的視圖控制器
[self.navigationController popViewControllerAnimated:YES];
}
- (IBAction)toroot:(UIButton *)sender {
//返回至根視圖控制器
[self.navigationController popViewControllerAnimated:YES];
}
- (IBAction)popto:(UIButton *)sender {
NSInteger index = [_field.text integerValue];
//下標不得超過數組的最大容器
if (index < self.navigationController.viewControllers.count) {
//返回到指定的視圖控制器 根據控制器下標從ViewControllers中獲取
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:index] animated:YES];
}
}
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:YES];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"111";
}
@end
屏幕快照 2016-03-09 下午8.45.28.png