Action sheet doesn't show Cancel button on iPad
Asked Answered
T

6

24

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];
    [actionSheet showInView:self.view];
    [actionSheet release];  
}

But on the iPad, only the destructive button shows.
What's the problem?

Thibaud answered 3/5, 2010 at 19:12 Comment(0)
T
1

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.

Thibaud answered 3/5, 2010 at 20:28 Comment(1)
Actually, it also seems to work with UIActionSheetStyleDefault - it's only UIActionSheetStyleAutomatic that hides the cancel button.Huey
S
43

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.

Slither answered 3/5, 2010 at 19:41 Comment(4)
Yes but under that paragraph, it then says "An animated action sheet should include a Cancel button, because people need to be able to dismiss the action sheet without closing the popover. Figure 4-14 shows an action sheet that appears in the location popover in Maps." That's what I'm trying to achieve.Thibaud
If you show the action sheet in a popover, does it still hide the cancel button? [sheet showInView:popOverController.view]Woken
I solved this by setting actionSheetStyle to UIActionSheetStyleBlackOpaque. I don't think it goes against the intent of the guidelines. Thanks for the link to the apple dev forum message.Thibaud
Update: as happens with the docs things have changed slightly, though the main thrust of this answer and Padawan's first comment are the same. The quote is now "Do not include a Cancel button when the action sheet is displayed without animation, because people can tap outside the popover to dismiss the action sheet without selecting one of the other alternatives." and can be found at a new URL.Beta
P
4

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.

Prophylaxis answered 12/11, 2011 at 10:37 Comment(0)
A
2

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.

Ashien answered 7/1, 2011 at 8:42 Comment(1)
Apparently, the cancelButtonIndex is not set correctly, i.e comparisons with the action sheet's cancel button index in delegate methods won't work as expected.Pillsbury
T
1

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.

Thibaud answered 3/5, 2010 at 20:28 Comment(1)
Actually, it also seems to work with UIActionSheetStyleDefault - it's only UIActionSheetStyleAutomatic that hides the cancel button.Huey
P
1

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;
Photochronograph answered 24/11, 2011 at 13:39 Comment(0)
P
1

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

Pucida answered 13/11, 2013 at 15:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.