How to provoke a modal view popup by rotating the device to landscape?

359 Views Asked by At

I am working on an App where I want to show a modal view controller when the user rotates the device. It should dismiss when the user rotates it back to portrait. During the rotation, the main view controller shouldn't be rotated.

How can I manage that?

I tried putting the code into the shouldAutorotateToInterfaceOrientation: method for showing the modal view controller, but I got always an error.

Some ideas?

2

There are 2 best solutions below

0
On BEST ANSWER

In the appDelegate you could use

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

and define the method

- (void) didRotate:(NSNotification *)notification

In this method, fetch the new orientation and apply or remove the modal view controller.

In shouldAutorotateToInterfaceOrientation: limit the original view to only accept the one orientation you want it to have.

0
On

you need to put this code in the

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

method of your view controller. Basically you will set your shouldAutorotateToInterfaceOrientationMethod: to return TRUE for any orientations you want to support (including the modal view popup). and then in the first method listed you implement the modal view controller becoming active.