发生该警告一般没有什么问题。但是当这个类是某个类的代理的时候,程序就会因为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];
}
}