Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIDeviceOrientationDidChangeNotification won't stop

I've got a little problem using UIDeviceOrientationDidChangeNotification. After I leave a certain ViewController (the viewWillDisappear: method is called), the device won't stop to send notifications.

This means, after I pushed another ViewController on top of the stack and I rotate the device, the receivedRotate: method of the ViewController here will be called and that I don't want.

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

My scenario is the following:

- (void)viewDidLoad {
    [super viewDidLoad];

     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedRotate:) name:UIDeviceOrientationDidChangeNotification object:nil];

     // other things...
}

Here the viewWillAppear: method

- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
}

And last the viewWillDisappear: method

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

Thanks in advance!

like image 733
schaechtele Avatar asked Dec 17 '22 22:12

schaechtele


1 Answers

In -viewWillDisappear:, remove the observer:

[[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
like image 128
Alex Reynolds Avatar answered Dec 28 '22 07:12

Alex Reynolds