Tono Nam
Tono Nam

Reputation: 36050

stop calling method generated by beginGeneratingDeviceOrientationNotifications

Let's say I am in the nib file someNib.xib . in order for me to be able to tell if the device orientation changed then I place this inside the view did load method:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientationChanged)
                                                 name:UIDeviceOrientationDidChangeNotification object:nil];

what this line does is that it calls the deviceOrientationChanged method overtime the device is rotated. my deviceOrientationChanged method looks like:

-(void) deviceOrientationChanged{

    if(self.view.frame.size.width<800)
    { 
      //means device is on portrait mode
    } else {

       //means device is on landscape mode
    }


}

so everything works great with this nib file. the problem comes in when I load another nib file as:

            // release allocated variables
            // VCHome is the name of the nib file
            VCHome *control = [VCHome alloc];
            [control initWithNibName:@"VCHome" bundle:nil];
            UINavigationController *navControl = [[UINavigationController alloc] initWithRootViewController:control];
            [self presentModalViewController:navControl animated:YES];
            [navControl setNavigationBarHidden:YES];
            [control release];
            [navControl release];

until this point everything works great. I am now on a new nib file and I can interact with VCHome.

why is it that when I am using the VCHome nib file and I rotate my device the deviceOrientationChanged method gets called? when I loaded the VCHome nib file should I release the someNib (last nib) so that this method does not get called. I don't plan to use someNib any more and would like my VCHome nib to behave like if I never loaded someNib.

Upvotes: 0

Views: 525

Answers (1)

adamsiton
adamsiton

Reputation: 3672

UIViewController has several methods you can implement that handle the life cycle of the orientation change for you:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

-(void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration

etc.

If you are using a simple UIViewControllers it's best to use these methods instead of registering for the event.

Also, if you need to check if the device orientation is landscape or portrait its better to use:

UIInterfaceOrientation currentOrientation = [[UIApplication sharedApplication] statusBarOrientation];
if (UIInterfaceOrientationIsLandscape(currentOrientation))
{
   ...
}

Upvotes: 3

Related Questions