本章講述內容如題。
類似於window,UINavigationController也有一個root view controller
在最上層的controller中的view會被顯示出來。
它包含一個root view controller, 一個topviewcontroller和一個UINavigationBar
MVC關係如下圖:
實例步驟:
1. 創建UINavigationController 實例,並把它設爲window的root controller
196
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
<span style="white-space:pre"> </span>self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds]];
<span style="white-space:pre"> </span>// Override point for customization after application launch
<span style="white-space:pre"> </span>BNRItemsViewController *itemsViewController = [[BNRItemsViewController alloc] init];
<span style="white-space:pre"> </span>// Create an instance of a UINavigationController
<span style="white-space:pre"> </span>// its stack contains only itemsViewController
<span style="white-space:pre"> </span>UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:itemsViewController];
<span style="white-space:pre"> </span>self.window.rootViewController = itemsViewController;
<span style="white-space:pre"> </span>// Place navigation controller's view in the window hierarchy
<span style="white-space:pre"> </span>self.window.rootViewController = navController;
<span style="white-space:pre"> </span>self.window.backgroundColor = [UIColor whiteColor];
<span style="white-space:pre"> </span>[self.window makeKeyAndVisible];
<span style="white-space:pre"> </span>return YES;
}
2. 添加一個UIViewController作爲第二個頁面,同時創建其xib文件
3. 添加控件並綁定到自己的controller
4. pushing view controller
5. 在tableviewcontroller中,
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
<span style="white-space:pre"> </span>BNRDetailViewController *detailViewController = [[BNRDetailViewController alloc] init];
<span style="white-space:pre"> </span>// Push it onto the top of the navigation controller's stack
<span style="white-space:pre"> </span>[self.navigationController pushViewController:detailViewController animated:YES];
}
6. 頁面之間數據傳遞
在UIViewController的viewWillAppear中綁定數據,viewWillDisappear中回寫數據。
源碼:https://github.com/ianzhengnan/Homepwner