preferredInterfaceOrientationForPresentation必须返回支持的界面方向(iOS 6)

我的应用程序窗口的根视图控制器是一个子类UINavigationController。 我已经将这个代码添加到类中:

- (BOOL)shouldAutorotate { return [self.topViewController shouldAutorotate]; } - (NSUInteger)supportedInterfaceOrientations { return [self.topViewController supportedInterfaceOrientations]; } - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { return [self.topViewController preferredInterfaceOrientationForPresentation]; } 

在我的根UIViewController我已经添加了这个代码:

 - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { return UIInterfaceOrientationPortrait; } - (BOOL)shouldAutorotate { return NO; } - (NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationPortrait; } 

当设备旋转到这个视图控制器上的横向时,我提出了一个模态视图控制器。 当设备旋转到肖像,我应该解雇模态视图,但是当我这样做时,我得到以下错误:

 'preferredInterfaceOrientationForPresentation must return a supported interface orientation!' 

为什么我得到这个错误?


我尝试从根UIViewController的shouldAutorotate返回YES,现在我得到错误“支持的方向与应用程序没有共同的方向,并且shouldAutorotate返回YES”。 这对我来说没有任何意义,因为UIInterfaceOrientationPortrait是应用程序支持的方向之一。

-supportedInterfaceOrientations ,您需要从UIInterfaceOrientationMask (而不是UIInterfaceOrientation返回值。 特别是,它看起来像你想UIInterfaceOrientationMaskPortrait

以下是-supportedInterfaceOrientations关于返回值的文档:

返回值

指定支持哪些方向的位掩码。 请参阅“UIInterfaceOrientationMask”获取有效的位掩码值。 此方法返回的值不能为0。