views:

169

answers:

1

Hey,

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!

+1  A: 

In -viewWillDisappear:, remove the observer:

[[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
Alex Reynolds
Thanks for your answer. It worked with the following solution: I added code to remove the observer in viewWillDisappear: but had to add the observer in viewWillAppear: because the second time the view will appear, the observer won't be added.But still, I don't understand the sense of beginGeneratingDeviceOrientationNotifications and endGeneratingDeviceOrientationNotifications. In my app, both have no effect on the firing of the notifications. The notifications are always fired?!?!
schaechtele