多控制器之一的UINavigationController如IPhone手机中的设置就是典型的UINavigationController实现
注意点:1,导航控制器的默认高度为44; 2,导航控制器有且只有一个根控制器 3,导航控制器是通过栈管理自控制器并遵循后进先出原则
展示UINavigationController控制器的简单实用
//
// AppDelegate.m
// 导航控制器
//
// Created by gaocai on 16/7/15.
// Copyright © 2016年 gaocai. All rights reserved.
//
#import "AppDelegate.h"
#import "ViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
//创建主窗口
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
//创建UINavigationController根控制器
ViewController *vc = [[ViewController alloc] init];
//创建UINavigationController控制器并将vc设置为根控制器
//注意UINavigationController是通过push创建和切换自控制器的所以也可以写成
/**
*
* UINavigationController *nc = [[UINavigationController alloc] init];
* [nc pushViewController:vc animated:YES];
*/
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:vc];
//为窗口设置根控制器
self.window.rootViewController = nc;
//显示
[self.window makeKeyAndVisible];
return YES;
}
演示自控制器之间的跳转
首先创建第二个控制器用于跳转,其次在ViewController.xib中拖出来一个UIButton处理点击调转
//
// ViewController.m
// 导航控制器
//
// Created by gaocai on 16/7/15.
// Copyright © 2016年 gaocai. All rights reserved.
//
#import "ViewController.h"
#import "TwoController.h"
@interface ViewController ()
@end
@implementation ViewController
- (IBAction)toTwo:(id)sender {
TwoController *two = [[TwoController alloc] init];
//通过push跳转控制器
[self.navigationController pushViewController:two animated:YES];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
控制器出栈常用函数,当不想使用系统出栈方式可使用以下函数操作
//当前控制器出栈...比如从A跳到B再到C,当在C页面调用时怎显示B
- (nullable UIViewController *)popViewControllerAnimated:(BOOL)animated;
//当前控制器出栈,显示指定控制器
- (nullable NSArray<__kindof UIViewController *> *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated;
//显示根控制器
- (nullable NSArray<__kindof UIViewController *> *)popToRootViewControllerAnimated:(BOOL)animated;