的iOS:警告“试图提出ViewController中谁的观点是不是在窗口层次结构”
我得到以下警告,当我试图呈现导航控制器上的ActivityController,的iOS:警告“试图提出ViewController中谁的观点是不是在窗口层次结构”
Attempt to present <UIActivityViewController: 0x15be1d60> on <UINavigationController: 0x14608e80> whose view is not in the window hierarchy!
我试图把视图控制器通过以下代码,
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
。您应该从最顶层的视图控制器本身提供一个视图控制器。
你需要改变:
UIViewController *vc = self.view.window.rootViewController;
[vc presentViewController: activityController animated: YES completion:nil];
到:
[self presentViewController: activityController animated: YES completion:nil];
您可以移动该方法调用另一个视图控制器viewDidAppear: – 2016-03-09 22:06:59
@TiagoAmaral:OP的问题是不同的,您的评论是无效的在当前的上下文。另外,如果您将该代码放在viewDidAppear中,则需要处理几个案例。 viewWill/DidAppear方法将被调用多次(比如在presentViewController关闭之后,在警告消除后等)。所以你需要处理这些情况,如果你把代码放在viewDidAppear中。 – 2016-03-10 05:20:36
在处理openUrl方法时我们可以做些什么? – 2016-06-04 13:39:35
分析: 由于本模式视图的ViewController类尚未加载到窗口。 这相当于建筑物,二楼都没有建好,直接去覆盖3楼,这绝对不是。 仅在加载ViewController的视图后;
的Python
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self showAlertViewController];
}
- (void)showAlertViewController {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Hello world" message:@"(*  ̄3)(ε ̄ *)d" preferredStyle:UIAlertControllerStyleAlert];
// Create the actions.
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"hello" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
NSLog(@"The \"Okay/Cancel\" alert's cancel action occured.");
}];
UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"world" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSLog(@"The \"Okay/Cancel\" alert's other action occured.");
}];
// Add the actions.
[alertController addAction:cancelAction];
[alertController addAction:otherAction];
UIWindow *windows = [[UIApplication sharedApplication].delegate window];
UIViewController *vc = windows.rootViewController;
[vc presentViewController:alertController animated: YES completion:nil];
}
这才是为我工作。
虽然这是对这个问题的回答,你能否进一步解释?此响应在其他用户访问时无效。 – 2016-05-24 09:00:31
根据“呈现”视图控制器生命中的时刻,您正试图呈现alertController,而“呈现VC”的视图尚未“附加”到窗口。 基本上,试图在ViewDidLoad中呈现另一个视图控制器将无法正常工作,而它将在ViewDidAppear方法中完美工作。 – FredericK 2017-07-06 14:47:39
更换行:
UIViewController *vc = self.view.window.rootViewController;
[vc presentViewController: activityController animated: YES completion:nil];
//to
[self presentViewController:activityController animated: YES completion:nil];
或
[self.navigationController pushViewController:activityController animated:YES];
非常感谢...我一直都看到过这个答案......但是请确保当你更大地使用这些代码时,从主队列中使用它而不是从任何子队列中使用它,否则可能会发生崩溃 – 2016-09-16 08:44:03
我面临着类似的问题在iPhone 6+。
在雨燕2.0
let obj = self.storyboard?.instantiateViewControllerWithIdentifier("navChatController") as! UINavigationController
obj.modalPresentationStyle = .FormSheet
obj.modalTransitionStyle = .CoverVertical
Constants.APPDELEGATE.window?.rootViewController?.presentViewController(obj, animated: false, completion: nil)
我通过这种方式解决它。
使用'[present presentViewController:activityController animated:YES completion:nil];' – 2015-02-07 06:32:19
@Mithun MP:是的。工作。谢啦。 :) – iOSNoob 2015-02-07 06:42:12