博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS pushViewController 和 presentViewController的区别 详解
阅读量:2393 次
发布时间:2019-05-10

本文共 1114 字,大约阅读时间需要 3 分钟。

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

转载地址:http://xpgab.baihongyu.com/

你可能感兴趣的文章
poj 2524 Ubiquitous Religions
查看>>
poj 1611 The Suspects
查看>>
poj 3331 The Idiot of the Year Contest!
查看>>
poj 3233 Matrix Power Series
查看>>
poj 3070 Fibonacci
查看>>
poj 1656 Counting Black
查看>>
BestCoder Round #28
查看>>
poj1845 Sumdiv
查看>>
poj3299 Humidex
查看>>
poj2159 Ancient Cipher
查看>>
poj1083 Moving Tables
查看>>
poj2255 Tree Recovery
查看>>
poj3904 Sky Code
查看>>
zoj 1745 Are We There Yet?
查看>>
UVA100 The 3n + 1 problem
查看>>
hdu1754 I Hate It
查看>>
hdu 1166 敌兵布阵(求区间的和,单节点更新)
查看>>
hiho一下 第四十四周 题目1 : 博弈游戏·Nim游戏
查看>>
poj2299 Ultra-QuickSort(线段树计数问题)
查看>>
poj3264 Balanced Lineup(求区间的最大值与最小值之差)
查看>>