【问题标题】:App crashing when dismissing a view controller in portrait关闭纵向视图控制器时应用程序崩溃
【发布时间】:2014-05-08 20:13:10
【问题描述】:

我有 viewcontroller X 和 viewcontroller Y。

viewController X 呈现 Y,Viewcontroller X 只能以纵向模式显示,但 Y 可以旋转到它想要的任何模式。

当我在横向模式下关闭 viewController Y 时,我收到以下错误。 排队:

[self dismissViewControllerAnimated:YES completion:nil];

错误: 由于未捕获的异常“UIApplicationInvalidInterfaceOrientation”而终止应用,原因:“preferredInterfaceOrientationForPresentation 必须返回受支持的界面方向!”

在 X 控制器中,我有:

-(BOOL)shouldAutorotate
{
    return NO;
}


- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
    return UIInterfaceOrientationPortrait;
}

在 Y 控制器中我有:

-(BOOL)shouldAutorotate
{
    return YES;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAllButUpsideDown;
}

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
    return UIInterfaceOrientationPortrait;
}

这在 ios 6 上运行良好,但现在在 xcode /ios7 中导致问题。现在,即使我在横向模式下关闭 Y 并返回 X,我也将其设置为更喜欢纵向,所以它不应该只强制纵向模式,而不管我的设备是否处于横向模式?

【问题讨论】:

  • 我不确定它是否会起作用,我想你已经尝试过了。但我建议将shouldAutorotate 中的shouldAutorotate 上的NO 更改为YESX
  • 不是这个,但我没试过。我尝试了同样的结果。原来我正在查看错误的文件,因为这是在选项卡视图控制器中,它正在寻找加载的最后一个视图的支持方向我添加了首选方法,它现在可以工作了。

标签: ios iphone objective-c uiviewcontroller


【解决方案1】:

在关闭子 viewController 之前添加:

[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationPortrait] forKey:@"orientation"];

假设你的父母是纵向的

【讨论】:

    【解决方案2】:

    在您的 shouldAutorotate 方法中返回 YES。也许你一直在错误的视图控制器中这样做。

    【讨论】:

    • 这似乎没有回答 OP 的问题,因为他们提到已经这样做但没有成功。您能否详细说明为什么您认为此答案会有所帮助?
    • 我相信现在确实如此。感谢您的提示。
    【解决方案3】:

    确保覆盖preferredInterfaceOrientationForPresentation: UIInterfaceOrientation,因为它会根据您的关闭视图控制器的旋转继承一个值。如果此值与您的视图控制器支持的界面方向不一致,则应用将崩溃。

        /// Returns `.portrait` as the default preferred interface orientation for presentation.
        ///
        /// - Important: It's important to explicitly set this value to a value which is aligned with `supportedInterfaceOrientations`. Not doing so can lead to crashes.
        /// In certain scenarios, if not set explicitly this value will be inherited from a dismissing view controller. The application will crash if the dismissing view controller was rotated to an orientation which isn't supported by
        /// this view controller. (*** Terminating app due to uncaught exception 'UIApplicationInvalidInterfaceOrientation', reason: 'preferredInterfaceOrientationForPresentation 'landscapeRight' must match a supported
        /// interface orientation: 'portrait'!')
        override public var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
            return .portrait
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-05
      • 2018-07-26
      • 2013-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-08
      相关资源
      最近更新 更多