ios - 关闭纵向 View Controller 时应用程序崩溃

标签 ios iphone objective-c uiviewcontroller

我有 viewcontroller X 和 viewcontroller Y。

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

当我在横向模式下关闭 viewController Y 时,出现以下错误。 行内:

[self dismissViewControllerAnimated:YES completion:nil];

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

在 X Controller 中,我有:

-(BOOL)shouldAutorotate
{
    return NO;
}


- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
    return UIInterfaceOrientationPortrait;
}

在 Y Controller 中我有:

-(BOOL)shouldAutorotate
{
    return YES;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAllButUpsideDown;
}

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
    return UIInterfaceOrientationPortrait;
}

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

最佳答案

在关闭子 viewController 之前添加:

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

假设你的 parent 是纵向的

关于ios - 关闭纵向 View Controller 时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23551608/

相关文章:

ios - 如何检查 indexPath 是否有效,从而避免 "attempt to scroll to invalid index path"错误?

ios - iphone 中的环境光传感器

iphone - indentationLevelForRowAtIndexPath 不缩进自定义单元格

iphone - NSDateFormatter 时区中带有冒号的日期/时间的日期格式

ios - 应用程序因无法识别的选择器发送到实例而崩溃 - 可能是骗局

iphone - 在另一个 UIView 下添加 subview

objective-c - CHDataStructures 是否符合 ARC 标准?

ios - 快速HTML解析

iOS 7 在单线程安全上没有合并冲突

ios - 将数据传递给 TabBar Controller