(1)UINavigaiongController的基本使用

建议先看
UInavigationController,UInavigationBar,UInavigationBarItem三者的区别

是什么 ? (导航控制器)

  1. UINavigationController 是一个容器类。里面盛放的是UIViewController。
  2. 这个容器在管理UIViewController时,遵循栈管理的原则(后进先出)。
  3. UINavigationController它由以下四部分组成:Navigation toolbar、Custom content、Navigation bar、,Navigation view。


    UINavigationController的组成

做什么用?

  1. 轻松管理多个控制器的切换!
  2. 导航控制器,以栈(先进后出)的形式管理控制器!

效果图


界面化创建

初始化UINavigaiongController

//第一种
UINavigationController *nav = [[UINavigationController alloc]init]; 
//第二种 (推荐)
//初始化 UINavigationController,同时添加底部子控制器
UINavigationController *nav  =
      [[UINavigationController alloc]initWithRootViewController:vc1];

添加子控制器(控制器跳转)

//第一种
[nav pushViewController:vc1 animated:YES];
//第二种(推荐)
nav.viewControllers = @[vc1,vc2];

获取所有子控制器

NSLog(@"%@",nav.viewControllers); //UINavigationController的属性 
NSLog(@"%@", nav.childViewControllers);//UIViewController的属性,只读

层次结构

@interface UIWindow : UIView
@interface UIScreen : NSObject <UITraitEnvironment>
@interface UIViewController : UIResponder <NSCoding, UIAppearanceContainer,
 UITraitEnvironment, UIContentContainer, UIFocusEnvironment>
@interface UINavigationController : UIViewController

//UIWindow的方法和属性
- (void)makeKeyAndVisible;
@property(nullable, nonatomic,strong) UIViewController *rootViewController

//UIView 的方法和属性
- (instancetype)initWithFrame:(CGRect)frame;
@property(nullable, nonatomic,copy) UIColor *backgroundColor

//UIScreen 的方法和属性
+ (UIScreen *)mainScreen;
@property(nonatomic,readonly) CGRect  bounds;

//UIViewController 的方法和属性
@property(null_resettable, nonatomic,strong) UIView *view;
@property(nonatomic,readonly) 
  NSArray<UIViewController*> * childViewControllers;//只读

//UINavigationController 的方法和属性
- (void)pushViewController:(UIViewController *)viewController 
  animated:(BOOL)animated;
@property(nonatomic,copy) NSArray<UIViewController *> *viewControllers;

完整代码

- (BOOL)application:(UIApplication *)application 
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//创建window
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    //初始化 UINavigationController(第一种)
    UINavigationController *nav = [[UINavigationController alloc]init];    
    //  创建子控制器
    UIViewController *vc1 = [[UIViewController alloc]init];
    vc1.view.backgroundColor = [UIColor orangeColor];
    
    UIViewController *vc2 = [[UIViewController alloc]init];
    vc2.view.backgroundColor = [UIColor yellowColor];

    //初始化 UINavigationController,同时添加底部子控制器(第二种)
    //UINavigationController *nav  = 
    //[[UINavigationController alloc]initWithRootViewController:vc1];

    // pushViewController 入栈
    //[nav pushViewController:vc1 animated:YES];
    //[nav pushViewController:vc2 animated:NO];
    
    nav.viewControllers = @[vc1,vc2];
    //获取所有子控制器
    NSLog(@"%@",nav.viewControllers); //UINavigationController的属性
    NSLog(@"%@", nav.childViewControllers);//UIViewController的属性,只读
    
    //设置成根控制器
    self.window.rootViewController = nav;   
    //显示window
    [self.window makeKeyAndVisible];  
    return YES;
}

推荐阅读更多精彩内容