iPhone UIActionSheet auto-rotating not working

流过昼夜 提交于 2019-12-23 10:40:49

问题


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?


回答1:


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];
}



回答2:


I found that I was running into this problem when I was presenting the action sheet in a delegate method from a subordinate view (which I had pushed using the navigation controller). The problem was that my view was not the current one, the subordinate view was still up at the point where I was trying to show the action sheet.

By changing my code a little bit so that the delegate method made a note of the interaction needed with the user, and deferring the action sheet presentation to this view’s viewDidAppear method, the sheet appeared at the proper time in the logical interface animation, and the auto-rotation problem went away. You might want to see if that helps you.

In other words, the flow became:

  • Subordinate view called delegate method to report choices the user made when leaving.
  • Parent view recorded this information for later.
  • Navigation controller was told to pop off subordinate view.
  • Parent view’s viewDidLoad: method detected the note made in the delegate method.
  • Action sheet was presented; rotation was now correct.


来源:https://stackoverflow.com/questions/4859352/iphone-uiactionsheet-auto-rotating-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!