xcode 应用程序试图在目标上呈现一个 nil 模态视图控制器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18461931/
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-09-15 03:44:06  来源:igfitidea点击:

Application tried to present a nil modal view controller on target

iosxcode

提问by Sinead

I have a simple single view app with two storyboards with different layouts for portrait and landscape. The landscape layout is completely different which is why I am using a second storyboard.

我有一个简单的单视图应用程序,其中包含两个故事板,纵向和横向布局各不相同。横向布局完全不同,这就是我使用第二个故事板的原因。

When I switch to landscape the view doesn't change and I get the

当我切换到横向时,视图不会改变,我得到了

"Application tried to present a nil modal view controller on target"

“应用程序试图在目标上呈现一个 nil 模态视图控制器”

` error in the debugger. I have checked that the landscape storyboard is referencing to the CalculatorViewController class.

` 调试器中的错误。我检查了风景故事板是否引用了 CalculatorViewController 类。

This is the line of code which generates the error:

这是生成错误的代码行:

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

Here is the whole method from CalculatorViewController.m:

这是来自 CalculatorViewController.m 的整个方法:

- (void)orientationChanged:(NSNotification *)notification

{    
    UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
        if (UIDeviceOrientationIsLandscape(deviceOrientation) &&
           !isShowingLandscapeView)        
    {
       // code here to show landscape storyboard
        UIStoryboard *landscapeStoryboard = [UIStoryboard storyboardWithName:@"LandscapeStoryboard" bundle:nil];

        CalculatorViewControllerLandscape *landscapeViewController = [landscapeStoryboard instantiateInitialViewController];
        [self presentViewController:landscapeViewController animated:YES completion:nil];
        isShowingLandscapeView = YES;
    }    
    else if (UIDeviceOrientationIsPortrait(deviceOrientation) &&             
         isShowingLandscapeView)        
    {        
        [self dismissViewControllerAnimated:YES completion:nil];        
        isShowingLandscapeView = NO;        
    }
}

The Initial View Controller is called CalculatorViewController. The View Controller for the landscape layout is called CalculatorViewControllerLandscape.

初始视图控制器称为 CalculatorViewController。横向布局的视图控制器称为 CalculatorViewControllerLandscape。

This is my first app so I really appreciate any help. I have tried to find a solution in similar questions posted without success.

这是我的第一个应用程序,所以我非常感谢任何帮助。我试图在没有成功发布的类似问题中找到解决方案。

回答by Gabriele Petronella

It means that landscapeViewControlleris nil.

这意味着landscapeViewControllernil

This can be cause by either:

这可能是由以下任一原因引起的:

  • landscapeStoryboardbeing nil(most likely because a storyboard named LandscapeStoryboardcannot be found)
  • no initial view controller being indicated in the storyboard.
  • landscapeStoryboard正在nil(很可能是因为LandscapeStoryboard找不到命名的故事板)
  • 故事板中没有指示初始视图控制器。

回答by JFS

You need to make sure that the identifier for the storyboard landscape-view is set to "LandscapeStoryboard". Select the storyboard and check the identifier.

您需要确保故事板横向视图的标识符设置为“LandscapeStoryboard”。选择故事板并检查标识符。