只要计算正在运行,就禁用DeviceOrientationNotification

时间:2013-08-15 20:27:00

标签: ios uiviewcontroller device-orientation

我的应用正在进行计算并在图表中显示结果。当设备旋转时,会生成一个新的UIViewController,在横向视图中显示图形。因此,必要的参数将传递给新的ViewController以创建图形。 当计算仍在运行时,当设备转为横向时,应用程序会崩溃。

是否有正确的方法暂时禁用使用过的DeviceOrientationNotification

-(void)calculate
{

disable DeviceOrientationNotification

...calculation code here

enable DeviceOrientationNotification again

} 

由于 (不要再打我,即使这个问题看起来很愚蠢)

1 个答案:

答案 0 :(得分:1)

在iOS 5和6中,UIView控制器上有一个回调用于自动旋转。我会在你开始计算时设置一个标志,你不应该自动旋转并在完成后将其设置回来。

//somewhere in .h or class extension or simple member variable
@property (nonatomic) BOOL shouldRotate;

// iOS 6
- (BOOL)shouldAutorotate {
    return self.shouldRotate;
}

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;//Return what is supported
}

// pre-iOS 6 support
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {

    return self.shouldRotate && //other interface check;
}

-(void)calculate{
     self.shouldRotate = NO;
     //do calculation
     self.shouldRotate = YES;
}