37

我得到以下警告,當我試圖呈現導航控制器上的ActivityController,的iOS:警告「試圖提出ViewController中誰的觀點是不是在窗口層次結構」

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

    }]; 

這是怎麼回事?

+2

使用'[present presentViewController:activityController animated:YES completion:nil];' – 2015-02-07 06:32:19

+0

@Mithun MP:是的。工作。謝啦。 :) – iOSNoob 2015-02-07 06:42:12

回答

33

您正試圖從rootViewController提供視圖控制器。在你的情況下,我認爲rootViewController不是當前的ViewController。無論您是在其上展示還是推出了新的UIViewController。您應該從最頂層的視圖控制器本身提供一個視圖控制器。

你需要改變:

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

到:

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

您可以移動該方法調用另一個視圖控制器viewDidAppear: – 2016-03-09 22:06:59

+2

@TiagoAmaral:OP的問題是不同的,您的評論是無效的在當前的上下文。另外,如果您將該代碼放在viewDidAppear中,則需要處理幾個案例。 viewWill/DidAppear方法將被調用多次(比如在presentViewController關閉之後,在警告消除後等)。所以你需要處理這些情況,如果你把代碼放在viewDidAppear中。 – 2016-03-10 05:20:36

+0

在處理openUrl方法時我們可以做些什麼? – 2016-06-04 13:39:35

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

這纔是爲我工作。

+1

雖然這是對這個問題的回答,你能否進一步解釋?此響應在其他用戶訪問時無效。 – 2016-05-24 09:00:31

+0

根據「呈現」視圖控制器生命中的時刻,您正試圖呈現alertController,而「呈現VC」的視圖尚未「附加」到窗口。 基本上,試圖在ViewDidLoad中呈現另一個視圖控制器將無法正常工作,而它將在ViewDidAppear方法中完美工作。 – FredericK 2017-07-06 14:47:39

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

非常感謝...我一直都看到過這個答案......但是請確保當你更大地使用這些代碼時,從主隊列中使用它而不是從任何子隊列中使用它,否則可能會發生崩潰 – 2016-09-16 08:44:03

-7

我面臨着類似的問題在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) 

我通過這種方式解決它。

相關問題