Action sheet doesn't show Cancel button on iPa

2019-01-23 12:26发布

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?

6条回答
来,给爷笑一个
2楼-- · 2019-01-23 12:42

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.

查看更多
不美不萌又怎样
3楼-- · 2019-01-23 12:47

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;
查看更多
该账号已被封号
4楼-- · 2019-01-23 12:49

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.

查看更多
乱世女痞
5楼-- · 2019-01-23 12:50

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

查看更多
一纸荒年 Trace。
6楼-- · 2019-01-23 12:56

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.

查看更多
混吃等死
7楼-- · 2019-01-23 12:59

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.

查看更多
登录 后发表回答