39

ナビゲーション コントローラーに ActivityController を表示しようとすると、次の警告が表示されます。

Attempt to present <UIActivityViewController: 0x15be1d60> on <UINavigationController: 0x14608e80> whose view is not in the window hierarchy!

次のコードでView Controllerを提示しようとしましたが、

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

からView Controllerを提示しようとしていrootViewControllerます。rootViewControllerあなたの場合、現在のViewControllerではないと思います。UIViewControllerその上に新しいものを提示またはプッシュしました。一番上のView Controller自体からView Controllerを提示する必要があります。

変更する必要があります:

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 クラスがウィンドウにロードされていないためです。これは建物に相当し、2 階は建てられていません。直接 3 階をカバーしてください。これは間違いありません。ViewController のビューを読み込んだ後でのみ。

パイソン

- (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 に答える
-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)

この方法で解決しました。

于 2016-06-07T04:32:19.617 に答える