2015-07-20 115 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]; 
    }]; 
}