pushViewController/presentModalViewController/addSubView区别及使用方法

本文深入探讨了iOS开发中的Swift编程语言,分享了实际项目中的优化策略与常见错误排查方法,旨在帮助开发者提高开发效率并确保应用的质量。
1.用UINavigationController的时候用pushViewController:animated
----返回之前的视图[[self navigationController] popViewControllerAnimated:YES];
---ps:push以后会在navigation的left bar自动添加back按钮,它的响应方法就是返回。所以一般不需要写返回方法,点back按钮即可。
2.其他时候用presentModalViewControll<wbr>er:animated</wbr>
<wbr>[self presentModalViewControll<wbr>er:controlleranimated:YES];//YES有动画效果</wbr></wbr>
-----返回之前的视图 [selfdismissModalViewControllerAnimated:YES];
3.切换视图一般用不到addSubview
UINavigationController是导航控制器,如果pushViewController的话,会跳转到下一个ViewController,点返回会回到现在这个ViewController;
如果是addSubview的话,其实还是对当前的ViewController操作,只是在当前视图上面又“盖”住了一层视图,其实原来的画面在下面呢,看不到而已。(当然,也可以用insertSubView atIndex那个方法设置放置的层次)。




另加一个

使用presentModalViewControllerAnimated方法从A->B->C,若想在C中直接返回A,则可这样实现:

C中返回事件:

voidback { [selfdismissModalViewControllerAnimated:NO];//注意一定是NO!! [[NSNotificationCenterdefaultCenter]postNotificationName:@"backback"object:nil]; }

然后在B中,

  1. //在viewdidload中:
  2. [[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(back)name:@"backback"object:nil];
  3. -(void)back
  4. {
  5. [selfdismissModalViewControllerAnimated:YES];
  6. }

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值