页面跳转的两个方法 1导航视图控制器 2模态

 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];

}

提示:A-->B  B-->C  逐级push
       C-->B  C-->A  有几个方法:

- (UIViewController *)popViewControllerAnimated:(BOOL)animated;

- (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated;

- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated;





2.模态跳转

//只有下面实现的时候有区别


-(void)rightButtonAction:(id)sender

{

    SecondViewController * second = [[SecondViewController alloc]init]; 

   //模态 用presentViewController

   [self presentViewController:second animated:0 completion:NULL];

    [second release];


}


如果从 SecondViewController 返回FirstViewController,

SecondViewController中的实现中用 dismissViewControllerAnimated
SecondViewController.m中

-(void)buttonAction:(id)sender

{

   [self dismissViewControllerAnimated:NO completion:NULL];


}

提示:模态控制只能A-->B,B-->C,  或者C-->B, B-->A 只能逐级跳转


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值