UIDeviceOrientationDidChangeNotification does not stop

I have a little problem using UIDeviceOrientationDidChangeNotification. After I left a specific ViewController (the viewWillDisappear :) method is called, the device will not stop sending notifications.

This means that after I clicked another ViewController on top of the stack and I turned the device, the receivedRotate method will be called here: the ViewController method, which I do not need.

I can not find something in the documentation and on other topics here. It would be great if someone could help.

My scenario is as follows:

- (void)viewDidLoad { [super viewDidLoad]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedRotate:) name:UIDeviceOrientationDidChangeNotification object:nil]; // other things... } 

Here viewWillAppear: method

 - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; } 

And the last: viewWillDisappear: method

 - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications]; } 

Thanks in advance!

+4
source share
1 answer

In -viewWillDisappear: remove the observer:

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

All Articles