IOS——页面跳转

来源:互联网 发布:幕墙破解软件大全 编辑:程序博客网 时间:2024/05/18 03:22

一、直接推到下一个页面

       定义好两个页面之后,在第一个界面添加一个button 并且对button实现changView方法

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. -(IBAction)chang:(id)sender;  

在 .m 文件里面实现
[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. -(void)chang:(id)sender{  
  2.     second *secondview = [[second alloc] initWithNibName:@"second" bundle:nil];  
  3.     [self presentViewController:secondview animated:YES completion:^{}];  
  4. }  

备注:second是第二个页面   这样就实现了第一种页面跳转,这种跳转的效果是从下面往上移动的效果

   页面返回

          当然,用这种方法来推到下一个页面,有时候还需要返回到上一个页面的时候,就需要额外实现一个方法 ,与  presentViewController:  对应的返回方法为dismissViewControllerAnimated:   在第二个页面添加一个button  对其添加一个方法,方法的实现里面加上这一行代码就可以了

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. [self presentViewController:firstview animated:YES completion:^{}];  
  2. [self dismissViewControllerAnimated:YES completion:^{}];  

二、导航条跳转 UINavigationController

       这个跳转方法需要的是借用UINavigationController来实现

    UINavigationController相当于是一个容器,然后将所有的view都放到这个容器里面去

        在代理.m 文件里面添加以下代码   

就是添加一个导航条  

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. UINavigationController *_navTest = [[UINavigationController alloc] initWithRootViewController:_viewController];  

导航条加进去之后第一个页面要加载哪一个?  用这一行代码来实现,并且是替换掉以前的那一个

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. self.window.rootViewController = _navTest;  

可以选择添加的代码:在  .m  加载的方法里面  添加这个页面的titile
[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. [self setTitle:@"first"];  

实现的方法
[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. -(void)chang:(id)sender{  
  2.     second *secondview = [[second alloc] initWithNibName:@"second" bundle:nil];  
  3.     [self.navigationController pushViewController:secondview animated:YES];  
  4. }  

           页面跳转之后,当然,自带的导航条上就有一个返回按钮,但是如果我们要自己代码实现这一个放回到上一个页面的话,我们用dismissViewControllerAnimated:是不能实现的,咋这里需要用的是popViewControllerAnimated  具体代码的实现为:
[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. [self.navigationController popViewControllerAnimated:YES];  

这样就完成了  两种页面的跳转和返回了

0 0
原创粉丝点击