处理iOS报错: Warning “attempt to present ViewController whose view is not in the window hierarchy”

        发生该警告一般没有什么问题。但是当这个类是某个类的代理的时候,程序就会因为xxx.delegate = nil,从而在调用代理方法的时候程序crash。

之所以出现这个问题,是因为present操作者虽然已经初始化完成,但是它的视图结构并没有加载完,所以发生“ whose view is not in the window hierarchy”。

当一个视图控制器处于viewwillApear,这个控制器的视图结构并没有加载完;而当它处于viewDidAppear的时候,该控制器的视图结构才正在处于window的结构体系中。

Apple 官网:

- viewWillAppear:

Notifies the view controller that its view is about to be added to a view hierarchy.


- viewDidAppear:

Notifies the view controller that its view was added to a view hierarchy.



所以,解决方法为:

         确保   “ [self presentViewController:self.presentVC animated:NO completion:nil];”语句放在viewDidAppear中或者已经ViewDidAppear完成之后。

因为我的情况是:A--- present  ----B---B处理完一定任务B dismiss然后---------->让A----present---C,所以我的处理方法为:


      (1)在B中增加一个实例变量presentVC,B任务完成后:

       [self dismissViewControllerAnimated:NO completion:nil];
       A.presentVC = C;
   

      (2)A的代码:

   

  - (void)viewDidAppear:(BOOL)animated
     {
         [super viewDidAppear:animated];
         if(self.presentVC){
        [self presentViewController:self.presentVC animated:NO completion:nil];
          }
     }



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值