Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone UIActionSheet auto-rotating not working

I read a lot about that. People say it will not autorotate whene its parent is not set to auto rotate. I tried everything but no luck. I created view-based app (v4.2) with a button that executes this:

    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Title" delegate:self cancelButtonTitle:@"Cancel Button" destructiveButtonTitle:@"Destructive Button" otherButtonTitles:@"Other Button 1", nil];
    actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
    [actionSheet showInView:self.view];

The root controller is set to auto-rotate. The actionSheet does not. Note that when I rotate the simulator none of the root controller's orientation methods are called. Is there a problem with the delegate? What is wrong?

like image 856
xpepermint Avatar asked Feb 01 '11 05:02

xpepermint


1 Answers

Well, here's my solution to this problem:

Basically what we do is:

  1. Listen to the rotation event.
  2. Listen to click event.
  3. Dismiss the actionSheet and present it again after the rotation is done. (we need to wait a small delay in order for it to take.

for example:

@interface ViewController ()
{
    UIActionSheet *_sheet;
    BOOL _isClicked;
}
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

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

- (IBAction)click:(id)sender
{
    _isClicked = YES;

    [self showActionSheet];
}

- (void)didRotate:(NSNotification *)note
{
    [_sheet dismissWithClickedButtonIndex:1 animated:YES];
    [self performSelector:@selector(showActionSheet) withObject:nil afterDelay:1.0];
}

- (void)showActionSheet
{
    if (!_isClicked) return;

    _sheet = [[UIActionSheet alloc] initWithTitle:@"title" delegate:self cancelButtonTitle:@"cancel" destructiveButtonTitle:@"new" otherButtonTitles:@"bla", nil];
    [_sheet showInView:self.view];
}
like image 103
Lirik Avatar answered Sep 28 '22 17:09

Lirik