我在使用 UIDeviceOrientationDidChangeNotification 时遇到了一点问题。当我离开某个 ViewController(调用 viewWillDisappear: 方法)后,设备不会停止发送通知。
这意味着,在我将另一个 ViewController 推到堆栈顶部并旋转设备后,此处的 ViewController 的 receivedRotate: 方法将被调用,而这是我不想要的。
我也无法在此处的文档和其他主题中找到任何内容。如果有人可以提供帮助,那就太好了。
我的场景如下:
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedRotate:) name:UIDeviceOrientationDidChangeNotification object:nil];
// other things...
}
这里是 viewWillAppear: 方法
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
}
最后是 viewWillDisappear: 方法
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
}
提前致谢!
最佳答案
在-viewWillDisappear:
中,移除观察者:
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
关于iphone - UIDeviceOrientationDidChangeNotification 不会停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3361166/