iOS:警告 "attempt to present ViewController whose view is not in the window hierarchy"
iOS: Warning "attempt to present ViewController whose view is not in the window 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");
}];
这里出了什么问题?
您正在尝试从 rootViewController
显示视图控制器。在你的情况下,我认为 rootViewController
不是当前的 ViewController。您在其上展示或推送了一个新的 UIViewController
。您应该从最顶层的视图控制器本身呈现一个视图控制器。
您需要更改:
UIViewController *vc = self.view.window.rootViewController;
[vc presentViewController: activityController animated: YES completion:nil];
至:
[self presentViewController: activityController animated: YES completion:nil];
分析:
因为目前模态视图ViewControllerclass还没有加载到window。
这就相当于盖楼了,二楼没盖,直接去盖三楼,这个肯定不行。
仅在加载 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];
}
这对我有用。
替换行:
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];
我在 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)
我是这样解决的
当我尝试在导航控制器上显示 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");
}];
这里出了什么问题?
您正在尝试从 rootViewController
显示视图控制器。在你的情况下,我认为 rootViewController
不是当前的 ViewController。您在其上展示或推送了一个新的 UIViewController
。您应该从最顶层的视图控制器本身呈现一个视图控制器。
您需要更改:
UIViewController *vc = self.view.window.rootViewController;
[vc presentViewController: activityController animated: YES completion:nil];
至:
[self presentViewController: activityController animated: YES completion:nil];
分析: 因为目前模态视图ViewControllerclass还没有加载到window。 这就相当于盖楼了,二楼没盖,直接去盖三楼,这个肯定不行。 仅在加载 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];
}
这对我有用。
替换行:
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];
我在 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)
我是这样解决的