導航條, 新建多個ViewControler 進行測試, NavigationgViewControler 含有返回上一級的封裝好的方法 BACK,不需要我們實現
我們可以通過 按鈕進行POP操作
首先是根 ViewCotroler
在window 上加入 NavigationController, 在加上一個 ViewController. 一直加到四個viewControler, 每一個上都添加一個button
前三個調用push方法,壓棧, 第四個進行測試。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[self.windowsetBackgroundColor:[UIColorwhiteColor]];
ViewController *viewCtl = [[ViewControlleralloc] init];
UINavigationController * rootNaviCtl = [[UINavigationControlleralloc] initWithRootViewController:viewCtl];
rootNaviCtl.delegate =self;
[self.windowsetRootViewController:rootNaviCtl];
[self.windowmakeKeyAndVisible];
return YES;
}
//最後一個ViewController上, 使用POP方法回到其他ViewController, 如果直接回到根ViewController, 棧中的也會相應的出棧。
//push 一個新的 ViewControler
ThirdViewControler *thirdViecCtl = [[ThirdViewControleralloc] init];
[self.navigationControllerpushViewController: thirdViecCtl animated:YES];
//直接退回到 rootViewcontroler
//[self.navigationController popToRootViewControllerAnimated:YES];
//回到上一個 ViewControler
//[self.navigationController popViewControllerAnimated:YES];
//回到指定的 ViewControler 在棧中根據index值去取出,不能重新建立一個新的viewController。
SecondViewControler *view = (SecondViewControler *)[self.navigationController.viewControllersobjectAtIndex:0];
[self.navigationControllerpopToViewController:view animated:YES];