UIDeviceOrientationDidChangeNotification不会停止

sch*_*ele 4 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...
}
Run Code Online (Sandbox Code Playgroud)

这里是viewWillAppear:方法

- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
}
Run Code Online (Sandbox Code Playgroud)

最后是viewWillDisappear:方法

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
}
Run Code Online (Sandbox Code Playgroud)

提前致谢!

Ale*_*lds 12

-viewWillDisappear:,删除观察者:

[[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
Run Code Online (Sandbox Code Playgroud)