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:
开发者_C百科 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?
Well, here's my solution to this problem:
Basically what we do is:
- Listen to the rotation event.
- Listen to click event.
- 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];
}
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.
精彩评论