1导航视图控制器
FirstViewController 跳转到 SecondViewController(先进后出)
在AppDelegate.m中设置根视图控制器
#import "AppDelegate.h"
#import "FirstViewController.h"
@implementation AppDelegate
- (void)dealloc
{
[_window release];
_window = nil;
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]autorelease];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
//创建根视图控制器(红色部分)
MainViewController * main = [[MainViewController alloc]init];
UINavigationController * aController = [[UINavigationController alloc]initWithRootViewController:main];
[_window setRootViewController:aController];
[main release];
[aController release];
[self.window makeKeyAndVisible];
return YES;
}
FirstViewController.m中代码
#import "SecondViewController.h"//引入头文件
- (void)viewDidLoad
{
[super viewDidLoad];
[self.navigationItem setTitle:@"第一页"];
self.navigationController.navigationBar.translucent = NO;
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"下一页" style:UIBarButtonItemStyleDone target:self action:@selector(rightButtonAction:)];
}
-(void)rightButtonAction:(id)sender
{
//由FirstViewController push 出SecondViewController
//导航控制器 用pushViewController
SecondViewController * second = [[SecondViewController alloc]init];
[self.navigationController pushViewController:second animated:YES];
[second release];
}
- (UIViewController *)popViewControllerAnimated:(BOOL)animated;
- (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated;
- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated;
//只有下面实现的时候有区别
-(void)rightButtonAction:(id)sender
{
SecondViewController * second = [[SecondViewController alloc]init];
//模态 用presentViewController
[self presentViewController:second animated:0 completion:NULL];
[second release];
}
-(void)buttonAction:(id)sender
{
[self dismissViewControllerAnimated:NO completion:NULL];
}
提示:模态控制只能A-->B,B-->C, 或者C-->B, B-->A 只能逐级跳转