iOS:警告“尝试呈现其视图不在窗口层次结构中的ViewController”

时间:2015-02-07 06:31:07

标签: ios objective-c uiactivityviewcontroller presentviewcontroller uiview-hierarchy

当我尝试在导航控制器上显示ActivityController时,我收到以下警告

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");

    }];

这里出了什么问题?

4 个答案:

答案 0 :(得分:34)

您正试图通过rootViewController展示视图控制器。在你的情况下,我认为rootViewController不是当前的ViewController。您可以在其上方展示或推送新的UIViewController。您应该从最顶层的视图控制器本身提供视图控制器。

您需要更改:

UIViewController *vc = self.view.window.rootViewController;
[vc presentViewController: activityController animated: YES completion:nil];

为:

[self presentViewController: activityController animated: YES completion:nil];

答案 1 :(得分:6)

分析: 因为当前的模态视图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];
}

这对我有用。

答案 2 :(得分:3)

替换行:

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];

答案 3 :(得分:-6)

我在iPhone 6+中遇到了类似的问题。

在Swift 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)

我这样解决了。