Tono Nam
Tono Nam

Reputation: 36058

execute method when iPhone enters landscape mode

Is there a delegate that will get called when the iPhone enters landscape or portrait mode? I need to change the style and place objects in a different place when the iPhone get's rotated. Do I have to do this with the accelerometer? Moreover if there exist such a delegate do I have to create the connection in interface builder. I am new to objective-c...

Upvotes: 3

Views: 1896

Answers (3)

Jhaliya - Praveen Sharma
Jhaliya - Praveen Sharma

Reputation: 31722

Register to listen for the orientation change notification.

UIDevice *device = [UIDevice currentDevice];
    //Tell it to start monitoring the accelerometer for orientation             
[device beginGeneratingDeviceOrientationNotifications];
    //Get the notification centre for the app   
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];    
[nc addObserver:self selector:@selector(orientationChanged:)        name:UIDeviceOrientationDidChangeNotification
         object:device];

Implement orientationChanged: method, which will be called when the device change the orientation. you could put code to check the orientation type and called your method.

- (void)orientationChanged:(NSNotification *)note
{
    NSLog(@"Orientation  has changed: %d", [[note object] orientation]);
}

Remove notification in dealloc.

[[NSNotificationCenter defaultCenter] removeObserver:self];
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];

Check the blog post

Reacting to iPhone's orientation

Upvotes: 7

Barum Rho
Barum Rho

Reputation: 2753

You can get UIDevice to generate notifications for orientation events. See the documentation for UIDevice.

If you need to detect the change at any moment, you might consider calling -beginGeneratingDeviceOrientationNotifications in your app's delegate.

Also, if you are using UIViewControllers, there are

  • shouldAutorotateToInterfaceOrientation:
  • willRotateToInterfaceOrientation: duration:
  • didRotateFromInterfaceOrientation:

Upvotes: 0

tassinari
tassinari

Reputation: 2363

Implement didRotateFromInterfaceOrientation in your view controller

-(void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation

Upvotes: 1

Related Questions