为什么在viewDidLoad中调用presentingViewController返回nil?

在iOS7的开发中,可以使用presentingViewController来获取模态 segue 前的控制器,但iOS8中实践发现此方法可能返回nil。这通常是由于在viewDidLoad时,视图控制器层级未加载完成。解决方案是将相关逻辑移到viewDidAppear之后,确保此时控制器已经被加载。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ios7应用开发入门经典(第7版)p256页提到,对于modal类型的segue,可以通过presentingViewController和presentedViewController获取segue前后的controller。

但在ios8中实践发现presentingViewController返回了nil. 这是什么情况?查阅stackoverflow,得到如下答案:


When viewDidLoad is called, there is no guarantee that the view controller hierarchy is loaded in the navigation tree. Moving the logic to a later stage (for example: viewWillAppear) should resolve that issue as presentingController should be loaded by then.


http://stackoverflow.com/questions/26081661/presentingviewcontroller-is-nil-when-using-presentviewcontrolleranimatedcomple


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值