present modal view on rotate

1.4k views Asked by At

So I have a UITableViewControler displaying a tableview in portrait mode.

As soon as i rotate the iPhone i want to present a modal view in landscape mode.

In the tableView i use:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

And to handle the present the modal view:

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation duration:(NSTimeInterval)duration {
        if((interfaceOrientation == UIInterfaceOrientationLandscapeRight) || (interfaceOrientation == UIInterfaceOrientationLandscapeLeft))
        {
            NSLog(@"Push page view");
            PagingViewController *s = [[PagingViewController alloc] initWithNibName:@"PagingView" bundle:nil];
            s.hidesBottomBarWhenPushed = YES;

            [self presentModalViewController:s animated:YES];
            [s release];
        }
}

The modal view i have the following:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

And to dismiss the modal view it self, I do:

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation duration:(NSTimeInterval)duration {
    if (interfaceOrientation == UIInterfaceOrientationPortrait)
    {
        NSLog(@"Dismiss my self");
        [self.parentViewController dismissModalViewControllerAnimated:YES];
    }
}

Some how this works two times. The third time i rotate the iPhone from Portrait mode to Landscape mode, i get a bad access error.

I cant figure out what gives me the error. Anyone care for a shot?

1

There are 1 answers

0
Martin On

The simplest way I can think of is to implement -shouldAutorotate... and dismiss the modal view and return NO to abort rotation. Perhaps that will be sufficient to avoid any concurrency issues. If this suggestion isn't to your liking take a look at NSNotificationCenter.