开发者

Action sheet doesn't show Cancel button on iPad

On the iphone, this code shows the cancel button:

- (IBAction)buttonPressed
{
    UIActionSheet *actionSheet = [[UIActionSheet alloc]
                                  initWithTitle:@"Are you sure?"
                                  delegate:self 
                                  cancelButtonTitle:@"No way!"
                                  destructiveButtonTitle:@"Yes, I'm sure!"
                                  otherButtonTitles:nil];开发者_StackOverflow社区
    [actionSheet showInView:self.view];
    [actionSheet release];  
}

But on the iPad, only the destructive button shows.

What's the problem?


This is part of the UI design and guidlines. Under 'Action Sheet' they say:

Do not include a Cancel button, because people can tap outside the popover to dismiss the action sheet without selecting one of the other alternatives.

It looks like the SDK hide the button for you on purpose. I'm not sure there is a solution, but maybe you could add your own button and set the cancelButtonIndex to match. Or you could switch to UIAlertView.


In iOS 5, this worked for me.

- (void)manualAddModel
{
    UIActionSheet *popupQuery = [[UIActionSheet alloc] initWithTitle: @"Select Equipment Type"
                                                            delegate: self
                                                   cancelButtonTitle: @"Cancel"
                                              destructiveButtonTitle: nil
                                                   otherButtonTitles: @"Add Boiler", @"Add Furnace",  nil];

    popupQuery.actionSheetStyle = UIActionSheetStyleDefault;
    [popupQuery addButtonWithTitle:@"Cancel"];
    [popupQuery showInView:self.view];
}


- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0)
    {
        NSLog(@"Add Boiler");
    }
    else if (buttonIndex == 1)
    {
        NSLog(@"Add Furnace");
    }
    else if (buttonIndex == 2)
    {
        NSLog(@"Cancel Button Clicked");
    }
}

Normally, tapping outside the actionsheet will serve the same purpose in iPad.


It looks like in iOS 4.2.1 you can manually add your own Cancel button like a normal button:

[actionSheet addButtonWithTitle:@"Cancel"];

And then set:

actionSheet.cancelButtonIndex = <your index>;

You won't get the red cancel button, but you will get either a blue or black one depending on your UIActionSheetStyle setting. In any event, it is pretty clearly distinguishable from the normal buttons and does correctly cancel.

Note that in my case I am showing an action sheet from within a popover controller, your results may vary in other scenarios.


I was able to solve this by setting the actionSheetStyle:

actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;

UIActionSheetStyleBlackTranslucent also works. I am displaying the action sheet from a modal view controller which I guess is not technically a "popovercontroller" like the guidelines say but not seeing a Cancel button on the action sheet doesn't look right when it appears on top of the modal view. All the user sees is one scary red button with no visible alternative.

Maybe I could change the modal view controller to a popovercontroller but then it wouldn't be modal which it needs to be.

--Update--

Well it was fun while it lasted but this no longer works in iOS 4.2.
I switched to using a UIAlertView instead of a UIActionSheet.
I no longer get a cool red button but it gets the job done.


I had the same issue when I tried to show ActionSheet in the View that was under another modal view, e.g. the view was invisible. Though the View wasn't nil looks like deep in framework it means so when it's not shown.

I solved the problem by setting a different UIModalPresentationStyle modalPresentationStyle property so the view became visible.

view.modalPresentationStyle = UIModalPresentationFormSheet;


According to iOS standard Cancel button is not displayed in UIActionSheet when displayed in iPad since UIActionSheet can be cancelled (Hide) by simply tapping any where outside ActionSheet region. In case of iPhone UIActionSheet will contain Cancel button.

Refer this link for further information UIActionSheet Cancel button in iPad

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜