導航控制器使用
創建
用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則返回點擊事件會失效