iphone - UIDeviceOrientationDidChangeNotification 不会停止

标签 iphone objective-c orientation

我在使用 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/

相关文章:

iphone - Finch音频引擎未打开默认的OpenAL设备

iphone - 如何在 iPhone 上访问 REST API?

ios - 在 swift 中使用 AFHTTPSessionManager

c# - 视觉树中的WP7弹出窗口不尊重方向

.net - 如何判断照片是横向拍摄还是纵向拍摄? JPEG .NET 元数据方向

ios - 表脚 View 按钮

ios - 跨多个 UIView 子类的单个 drawRect 实现

ios - 尝试启动 MapKit 位置更新而不提示在 ios 9 中使用 objective-c 进行位置授权

iphone - 确定 iPad 上的 UIInterfaceOrientation

iphone - 为什么会崩溃:stringByAppendingFormat