导航视图
1、创建导航视图根视图:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen] bounds]]; ViewController* viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; self.navigationController = [[UINavigationController alloc] initWithRootViewController:viewController]; self.window.rootViewController = self.navigationController; [self.window makeKeyAndVisible]; return YES; }
2、跳转到新视图控制器:
SecondViewController* secondView = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil]; [self.navigationController pushViewController:secondView animated:nil];
3、返回之前的视图控制器:
//回到上一级视图 // [self.navigationController popViewControllerAnimated:YES]; //回到根视图 // [self.navigationController popToRootViewControllerAnimated:YES]; //回到指定视图 NSArray * ctrlArray = self.navigationController.viewControllers; [self.navigationController popToViewController:[ctrlArray objectAtIndex:0] animated:YES];
备注:这里各个压入堆栈中的ViewController 都采用self.navigationController获取导航控制器。可参考下文:
http://kingbinchow.iteye.com/blog/1990807