39

当我尝试在导航控制器上显示 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

4 回答 4

35

您正在尝试从rootViewController. 在您的情况下,我认为这rootViewController不是当前的 ViewController。您要么在其上展示或推新UIViewController。您应该从最顶层的视图控制器本身呈现一个视图控制器。

你需要改变:

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

至:

[self presentViewController: activityController animated: YES completion:nil];
于 2015-02-07T08:03:52.423 回答
6

分析:因为当前模态视图ViewController类还没有加载到窗口中。这相当于楼,二楼还没建,直接去盖三楼,这个肯定不行。只有在加载 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-24T08:55:35.320 回答
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];
于 2016-05-25T07:29:19.167 回答
-6

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

我通过这种方式解决了它。

于 2016-06-07T04:32:19.617 回答