iOS:警告“尝试呈现其视图不在窗口层次结构中的ViewController” [英] iOS: Warning "attempt to present ViewController whose view is not in the window hierarchy"
问题描述
当我尝试在导航控制器上显示ActivityController时,我收到以下警告,
I am getting following warning when I try to present a ActivityController on navigation controller,
Attempt to present <UIActivityViewController: 0x15be1d60> on <UINavigationController: 0x14608e80> whose view is not in the window hierarchy!
我试图通过以下代码呈现视图控制器,
I have tried to present view controller by following code,
UIActivityViewController * activityController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:applicationActivities];
activityController.excludedActivityTypes = excludeActivities;
UIViewController *vc = self.view.window.rootViewController;
[vc presentViewController: activityController animated: YES completion:nil];
[activityController setCompletionHandler:^(NSString *activityType, BOOL completed) {
NSLog(@"completed");
}];
这里出了什么问题?
推荐答案
您正在尝试从 rootViewController
中呈现视图控制器。在你的情况下,我认为 rootViewController
不是当前的ViewController。您在其上面呈现或推送了新的 UIViewController
。您应该从最顶层的视图控制器本身提供一个视图控制器。
You are trying to present a view controller from the rootViewController
. In your case I think the rootViewController
is not the current ViewController. Either you presented or pushed a new UIViewController
on top of it. You should present a view controller from the top most view controller itself.
您需要更改:
UIViewController *vc = self.view.window.rootViewController;
[vc presentViewController: activityController animated: YES completion:nil];
to:
[self presentViewController: activityController animated: YES completion:nil];
这篇关于iOS:警告“尝试呈现其视图不在窗口层次结构中的ViewController”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!