23

在 iphone 上,此代码显示取消按钮:

- (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];  
}

但在 iPad 上,只显示破坏性按钮。
有什么问题?

4

6 回答 6

42

这是UI 设计和指南的一部分。在“行动表”下,他们说:

不要包含取消按钮,因为人们可以在弹出框外部点击以关闭操作表,而无需选择其他选项之一。

看起来SDK 故意为您隐藏按钮。我不确定是否有解决方案,但也许您可以添加自己的按钮并将其设置cancelButtonIndex为匹配。或者你可以切换到UIAlertView.

于 2010-05-03T19:41:55.220 回答
4

在 iOS 5 中,这对我有用。

- (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");
    }
}

通常,在操作表之外点击将在 iPad 中起到相同的作用。

于 2011-11-12T10:37:28.137 回答
2

看起来在 iOS 4.2.1 中您可以像普通按钮一样手动添加自己的取消按钮:

[actionSheet addButtonWithTitle:@"Cancel"];

然后设置:

actionSheet.cancelButtonIndex = <your index>;

您不会看到红色的取消按钮,但会根据您的UIActionSheetStyle设置获得蓝色或黑色的取消按钮。无论如何,它与普通按钮的区别非常明显,并且可以正确取消。

请注意,在我的情况下,我正在显示弹出框控制器中的操作表,您的结果在其他情况下可能会有所不同。

于 2011-01-07T08:42:42.913 回答
1

我可以通过设置 actionSheetStyle 来解决这个问题:

actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;

UIActionSheetStyleBlackTranslucent 也可以。我正在从模态视图控制器显示操作表,我猜这在技术上并不是指南所说的“popovercontroller”,但是当它出现在模态视图顶部时,在操作表上没有看到取消按钮看起来不正确。用户看到的只是一个可怕的红色按钮,没有可见的替代品。

也许我可以将模态视图控制器更改为 popovercontroller 但它不会是它需要的模态。

- 更新 -

好吧,这很有趣,但它在 iOS 4.2 中不再有效。
我切换到使用 UIAlertView 而不是 UIActionSheet。
我不再得到一个很酷的红色按钮,但它完成了工作。

于 2010-05-03T20:28:32.177 回答
1

当我试图在另一个模态视图下的视图中显示 ActionSheet 时,我遇到了同样的问题,例如视图是不可见的。虽然 View 不是 nil 看起来像在框架中很深,但这意味着当它没有显示时。

我通过设置不同的属性解决了这个问题,UIModalPresentationStyle modalPresentationStyle因此视图变得可见。

view.modalPresentationStyle = UIModalPresentationFormSheet;
于 2011-11-24T13:39:39.157 回答
1

根据 iOS 标准,当 UIActionSheet 显示在 iPad 中时,取消按钮不会显示在 UIActionSheet 中,因为 UIActionSheet 可以通过简单地点击 ActionSheet 区域外的任何位置来取消(隐藏)。如果是 iPhone UIActionSheet 将包含取消按钮。

有关更多信息,请参阅此链接iPad 中的 UIActionSheet 取消按钮

于 2013-11-13T15:05:10.393 回答