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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28379327/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 04:43:31  来源:igfitidea点击:

iOS: Warning "attempt to present ViewController whose view is not in the window hierarchy"

iosobjective-cuiactivityviewcontrollerpresentviewcontrolleruiview-hierarchy

提问by iOSNoob

I am getting following warning when I try to present a ActivityController on navigation controller,

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

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

    }];

Whats going wrong here ?

这里出了什么问题?

采纳答案by Midhun MP

You are trying to present a view controller from the rootViewController. In your case I think the rootViewControlleris not the current ViewController. Either you presented or pushed a new UIViewControlleron top of it. You should present a view controller from the top most view controller itself.

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

You need to change:

你需要改变:

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

to:

到:

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

回答by qingsong

Analysis: Because the present modal view ViewController class has not been loaded into the window. This is equivalent to the building, second floor haven't built, directly go to cover 3 floor, this is definitely not. Only after load ViewController's view ;

分析: 因为当前模态视图ViewController类还没有加载到窗口中。这相当于建了,二楼还没建,直接去盖3楼,这绝对不行。只有在加载 ViewController 的视图之后;

Python

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

This's worked for me.

这对我有用。

回答by Bhadresh Kathiriya

Replace line:

替换线:

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

or

或者

[self.navigationController pushViewController:activityController animated:YES];

回答by Abdul Hameed

I faced the similar issue in iPhone 6+.

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

In Swift 2.0

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

I solved it by this way.

我是这样解决的。