导航控制器使用
创建
用xcode带的功能进行拖拽
- 拖拽Navigation Controller
- 或者选中原有的,点击上面的Editor-> embed in-> Navigation Controller
使用代码的方式,在AppDelegate.m中
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// 1. 创建一个现实的UIWindow,大小与屏幕一样
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
// 2. 创建一个显示的控制器,使用三种方式
UIViewController* vc;
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UINavigationController * controll = [[UINavigationController alloc]initWithRootViewController:vc];
// 3. 将控制器与window关联
self.window.rootViewController = controll;
// 4. 将window显示出来
[self.window makeKeyAndVisible];
return YES;
}
返回
- 弹出一个栈
[self.navigationController popViewControllerAnimated:YES];
- 弹出根栈
[self.navigationController popToRootViewControllerAnimated:YES];
- 弹出到指定栈
UIViewController* targetVc = self.navigationController.childViewControllers[1];
[self.navigationController popToViewController: targetVc animated:YES];
一些导航控制器的属性
- xcode设置属性
在View Controller下面的View下面有个Navigation Item设置title - 在代码中设置
// 设置导航标题
self.navigationItem.title = @"hahaha";
// 设置导航图片
self.navigationItem.titleView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"9_daye"]];
- 导航栏左右放置的是Bar Button Item
- 如果代码中设置了leftBarButtonItem则返回点击事件会失效