views:

863

answers:

2

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?

+4  A: 

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.

Colin Gislason
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.
Padawan
If you show the action sheet in a popover, does it still hide the cancel button? `[sheet showInView:popOverController.view]`
Squeegy
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.
Padawan
+2  A: 

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.

Padawan
Actually, it also seems to work with UIActionSheetStyleDefault - it's only UIActionSheetStyleAutomatic that hides the cancel button.
Psionides