UIDeviceOrientationDidChangeNotification不会停止

时间:2010-07-29 09:46:30

标签: 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];
}

提前致谢!

1 个答案:

答案 0 :(得分:12)

-viewWillDisappear:中,删除观察者:

[[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];