Day.03.09 导航控制栏

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
Day.03.09 导航控制栏_第1张图片
屏幕快照 2016-03-09 下午8.45.28.png

你可能感兴趣的:(Day.03.09 导航控制栏)