尝试在视图不在窗口层次结构中的UIViewController上呈现UIViewController

本文翻译自:Attempt to present UIViewController on UIViewController whose view is not in the window hierarchy

Just started using Xcode 4.5 and I got this error in the console: 刚开始使用Xcode 4.5,我在控制台中收到此错误:

Warning: Attempt to present < finishViewController: 0x1e56e0a0 > on < ViewController: 0x1ec3e000> whose view is not in the window hierarchy! 警告:尝试在其视图不在窗口层次结构中的<ViewController:0x1ec3e000>上显示<finishViewController:0x1e56e0a0>!

The view is still being presented and everything in the app is working fine. 该视图仍在显示,应用程序中的所有内容均正常运行。 Is this something new in iOS 6? 这是iOS 6的新功能吗?

This is the code I'm using to change between views: 这是我用来在视图之间进行更改的代码:

UIStoryboard *storyboard = self.storyboard;
finishViewController *finished = 
[storyboard instantiateViewControllerWithIdentifier:@"finishViewController"];

[self presentViewController:finished animated:NO completion:NULL];

#1楼

参考:https://stackoom.com/question/nm4p/尝试在视图不在窗口层次结构中的UIViewController上呈现UIViewController


#2楼

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. 我在尝试在viewDidLoad方法中呈现模式视图控制器时遇到了一个问题。 The solution for me was to move this call to the viewDidAppear: method. 对我来说,解决方案是将此调用移至viewDidAppear:方法。

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). 我的假设是视图控制器的观点是不是在窗口的视图层次的点,它已经被加载(在当viewDidLoad发送的消息),但它在窗口层次结构已经呈现后(当viewDidAppear:消息已发送)。


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... 如果您确实在viewDidAppear:调用了presentViewController:animated:completion: ,则可能会遇到一个问题,即无论何时出现视图控制器的视图,模态视图控制器总是被显示(这很有意义!),所以模态视图控制器被呈现将永远不会消失...

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. 也许这不是呈现模态视图控制器的最佳位置,或者可能需要保留一些其他状态,以允许呈现视图控制器决定是否应立即呈现模态视图控制器。


#3楼

I also encountered this problem when I tried to present a UIViewController in viewDidLoad . 当我尝试在viewDidLoad呈现UIViewController时,我也遇到了此问题。 James Bedford's answer worked, but my app showed the background first for 1 or 2 seconds. 詹姆斯·贝德福德(James Bedford)的答案奏效,但我的应用程序先显示背景1或2秒。

After some research, I've found a way to solve this using the addChildViewController . 经过研究,我找到了一种使用addChildViewController解决此问题的方法。

- (void)viewDidLoad
{
    ...
    [self.view addSubview: navigationViewController.view];
    [self addChildViewController: navigationViewController];
    ...
}

#4楼

viewWillLayoutSubviews and viewDidLayoutSubviews (iOS 5.0+) can be used for this purpose. viewWillLayoutSubviewsviewDidLayoutSubviews (iOS 5.0+)可以用于此目的。 They are called earlier than viewDidAppear. 它们的调用早于viewDidAppear。


#5楼

Probably, like me, you have a wrong root viewController 可能像我一样,您的root viewController错误

I want to display a ViewController in a non-UIViewController context, 我想在non-UIViewController上下文中显示ViewController

So I can't use such code: 所以我不能使用这样的代码:

[self presentViewController:]

So, I get a UIViewController: 因此,我得到一个UIViewController:

[[[[UIApplication sharedApplication] delegate] window] rootViewController]

For some reason (logical bug), the rootViewController is something other than expected (a normal UIViewController ). 出于某种原因(逻辑错误), rootViewController超出了预期的范围(普通的UIViewController )。 Then I correct the bug, replacing rootViewController with a UINavigationController , and the problem is gone. 然后,我纠正了该错误,用UINavigationController替换了rootViewController ,问题消失了。


#6楼

I just had this issue too, but it had nothing to do with the timing. 我也有这个问题,但是与时间无关。 I was using a singleton to handle scenes, and I set it as the presenter. 我当时使用单例来处理场景,因此将其设置为演示者。 In other words "Self" wasn't hooked up to anything. 换句话说,“自我”与任何事物都不相关。 I just made its inner "scene" the new presenter and voila, it worked. 我只是将其内部的“场景”设置为新的演示者,瞧,它起作用了。 (Voila loses its touch after you learn its meaning, heh). (Voila在了解其含义后会失去联系,呵呵)。

So yeah, it's not about "magically finding the right way", it's about understanding where your code stands and what it's doing. 是的,这不是要“神奇地找到正确的方式”,而是要了解代码的位置及其作用。 I'm happy Apple gave such a plain-English warning message, even with emotion to it. 我很高兴Apple发出了这样一个简单的英语警告信息,即使对此感到激动。 Kudos to the apple dev who did that!! 致敬那些做到这一点的苹果开发人员!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值