您的位置:首页 > 移动开发 > IOS开发

whose view is not in the window hierarchy

2016-07-10 22:43 405 查看
问题描述:有A、B两个view controller,在A的viewDidLoad方法中,调用presentViewController去present B View Controller,出现”whose view is not in the window hierarchy” warning,且没有正确跳转。

问题解释:
stack overflow 回答原文如下: 

Where are you calling this method from? I had an issue where I was attempting to present a modal view controller within the 
viewDidLoad
 method.
The solution for me was to move this call to the 
viewDidAppear:
 method.

My presumption is that the view controller's view is
not in the window's view hierarchy at the point that it has been loaded (when the 
viewDidLoad
 message
is sent), but it is in the window hierarchy after it has been presented (when the 
viewDidAppear:
 message
is sent).

Caution

If you do make a call to 
presentViewController:animated:completion:
 in
the 
viewDidAppear:
 you
may run into an issue whereby the modal view controller is always being presented whenever the view controller's view appears (which makes sense!) and so the modal view controller being presented will never go away...

Maybe this isn't the best place to present the modal view controller, or perhaps some additional state needs to be kept
which allows the presenting view controller to decide whether or not it should present the modal view controller immediately.

stack overflow 链接:http://stackoverflow.com/questions/11862883/whose-view-is-not-in-the-window-hierarchy

解决方案:不要在A的viewDidLoad方法中,调用presentViewController来present B View Controller,而是在viewDidAppear中。理由是:viewDidLoad调用时,此时view controller’s view 并不在window 的view hierarchy中。

遗留问题:也许viewDidAppear中并不是最合适的转场地方。 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  iOS