我正在使用设备方向通知来了解设备是否已旋转,以便我可以执行选择器。这是我正在使用的代码:
- (void)awakeFromNib
{
isShowingLandscapeView = NO;
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
}
- (void)orientationChanged:(NSNotification *)notification
{
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
if (UIDeviceOrientationIsLandscape(deviceOrientation) &&
!isShowingLandscapeView)
{
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];
[self performSegueWithIdentifier:@"landscape" sender:self];
isShowingLandscapeView = YES;
}
else if (UIDeviceOrientationIsPortrait(deviceOrientation) &&
isShowingLandscapeView)
{
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];
[self dismissViewControllerAnimated:YES completion:nil];
isShowingLandscapeView = NO;
}
}
但是,当我导航到其他子视图并旋转设备时,尽管上面的代码位于一个独立于其他视图所具有的类的类中,但仍会执行选择器。那我怎么能阻止呢?我试过:
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
它也没有用。当设备旋转时,我的应用程序实际上会以横向模式弹出另一个视图。所以在子视图中,横向视图出现在我旋转设备中。我该如何解决?