iOS pushViewController 和 presentViewController的区别 详解

来源:互联网 发布:天下全與总图 知乎 编辑:程序博客网 时间:2024/06/05 18:35
pushViewController 导航控制器入栈的方式切换页面
presentViewController 模态切换的方式切换页面

1> 用 UINavigationController 的时候用 pushViewController:animated
     返回之前的视图 [[self navigationController] popViewControllerAnimated:YES];
     push 以后会在 navigation的 left bar自动添加back按钮,它的响应方法就是返回,所以一般不需要写返回方法,点back按钮即可

2> 其他时候用presentModalViewController:animated
     [self presentModalViewController:controller animated:YES];// YES有动画效果
     返回之前的视图 [self dismissModalViewControllerAnimated:YES];  

3> 切换视图一般用不到 addSubview
UINavigationController是导航控制器,如果pushViewController的话,会跳转到下一个ViewController,点返回会回到现在这个ViewController;
如果是addSubview的话,其实还是对当前的ViewController操作,只是在当前视图上面又“盖”住了一层视图,其实原来的画面在下面呢,看不到而已。(当然,也可以用insertSubView atIndex那个方法设置放置的层次)。

案例 :
使用 presentModalViewControllerAnimated方法从 A -> B -> C,若想在 C 中直接返回 A,则可这样实现
C中返回事件 :
- (void)back  
{
    [self dismissModalViewControllerAnimated:NO]; // 注意一定是NO
    [[NSNotificationCenter  defaultCenter]postNotificationName:@"backback" object:nil];  
}

然后在B中 :
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(back) name:@"backback" object:nil];  

-(void)back  
{  
     [self dismissModalViewControllerAnimated:YES];  
}

0 0
原创粉丝点击