2015-07-20 165 views
0

在模態創建的視圖中,按下按鈕會導致模態視圖關閉,並加載另一個模態視圖。從另一個模態視圖創建模態視圖失敗

DenkoStation[4259:73173] Warning: Attempt to present <LanguageSelectionViewController: 0x7b185430> on <ViewController: 0x79f52e50> whose view is not in the window hierarchy! 

最讓我驚訝的是,之前我做了一些改變,以我的代碼as outlined here代碼愉快地運行的事實:當這個代碼塊執行拋出

- (void)loadLanguageSelectionView { 
    [self dismissViewControllerAnimated:YES completion:nil]; 
    UIViewController *languageSelectionController = [[LanguageSelectionViewController alloc] initWithNibName:nil bundle:nil]; 
    [languageSelectionController setModalPresentationStyle:UIModalPresentationCustom]; 
    [languageSelectionController setModalTransitionStyle:UIModalTransitionStyleCrossDissolve]; 
    [self presentViewController:languageSelectionController animated:YES completion:nil]; 
} 

以下錯誤。

哪裏出錯?

+1

您關閉該視圖控制器後,它不再是層次結構,所以錯誤。如果您首先獲得呈現視圖控制器,然後在調用'presentViewController:'時將其用作接收器,會怎麼樣? (不是一個答案,因爲我沒有嘗試過......但它似乎是合理的。) –

+0

問題是你先解僱'self',使用'[self dismissViewControllerAnimated:YES completion:nil];'然後你試着提出'self'上的'languageSelectionController'。這就是爲什麼如果失敗。 – x4h1d

+0

@PhillipMills它的工作原理。謝謝。如果您將解決方案寫成答案,我會選擇它作爲正確答案。 –

回答

1

因爲您正嘗試在viewController上呈現一個viewController,該viewController已經被解散並且不再處於窗口層次結構中。

什麼你可以嘗試,你可以從當前的viewController採取ParentViewController引用,然後你可以在ParentViewController提出新的viewController是這樣的:

- (void)loadLanguageSelectionView { 
    UIViewController *parentController = self.presentingViewController; 
    [self dismissViewControllerAnimated:YES completion:^{ 
     UIViewController *languageSelectionController = [[LanguageSelectionViewController alloc] initWithNibName:nil bundle:nil]; 
     [languageSelectionController setModalPresentationStyle:UIModalPresentationCustom]; 
     [languageSelectionController setModalTransitionStyle:UIModalTransitionStyleCrossDissolve]; 
     [parentController presentViewController:languageSelectionController animated:YES completion:nil]; 
    }]; 
}