取消按钮和 UIActionSheet 的问题

发布于 2024-10-20 04:21:39 字数 931 浏览 1 评论 0原文

如何确定 UIActionSheet 上的取消按钮是否被按下?

我的 UIActionSheet 设置如下:

-(IBAction)fileButtonPressed
{
    UIActionSheet *mymenu = [[UIActionSheet alloc] 
                             initWithTitle:@"Select Folder" 
                             delegate:self 
                             cancelButtonTitle:@"Cancel" 
                             destructiveButtonTitle:nil 
                             otherButtonTitles:nil];

    for (i=0; i<3; i++) 
    { 
        [mymenu addButtonWithTitle:@"Button Name"]; 
    }

    [mymenu showInView:self.view];

}

我遇到的问题是我无法区分取消按钮和选择的第一个按钮。

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{  
    NSString *option = [actionSheet buttonTitleAtIndex:buttonIndex];

    //buttonIndex == 0 if the cancel button is pressed or 
    //if the first item is pressed.
}

有更好的方法来设置这个吗?

How do I determine if the cancel button was pressed on a UIActionSheet?

My UIActionSheet is set up like this:

-(IBAction)fileButtonPressed
{
    UIActionSheet *mymenu = [[UIActionSheet alloc] 
                             initWithTitle:@"Select Folder" 
                             delegate:self 
                             cancelButtonTitle:@"Cancel" 
                             destructiveButtonTitle:nil 
                             otherButtonTitles:nil];

    for (i=0; i<3; i++) 
    { 
        [mymenu addButtonWithTitle:@"Button Name"]; 
    }

    [mymenu showInView:self.view];

}

The problem that I have is that I cannot differentiate between the cancel button and the first button selected.

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{  
    NSString *option = [actionSheet buttonTitleAtIndex:buttonIndex];

    //buttonIndex == 0 if the cancel button is pressed or 
    //if the first item is pressed.
}

Is there a better way of setting this up?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ヤ经典坏疍 2024-10-27 04:21:40

事实证明,诀窍是不使用自动取消按钮,而是自己添加它。

另一个小问题是在末尾而不是开头添加取消按钮。

-(IBAction)fileButtonPressed
{
    UIActionSheet *mymenu = [[UIActionSheet alloc] 
                             initWithTitle:@"Select Folder" 
                             delegate:self 
                             cancelButtonTitle:nil 
                             destructiveButtonTitle:nil 
                             otherButtonTitles:nil];
    for (int nb=0; nb<3; nb++) 
    { 
        [mymenu addButtonWithTitle:@"Button Name"]; 
    }

    mymenu.cancelButtonIndex = [mymenu addButtonWithTitle: @"Cancel"];

    [mymenu showInView:self.view];
}

归功于此 stackoverflow 条目的答案。

The trick turns out to be not to use the automatic cancel button but to add it yourself.

The other slight gotcha is to add the cancel button at the end and not at the beginning.

-(IBAction)fileButtonPressed
{
    UIActionSheet *mymenu = [[UIActionSheet alloc] 
                             initWithTitle:@"Select Folder" 
                             delegate:self 
                             cancelButtonTitle:nil 
                             destructiveButtonTitle:nil 
                             otherButtonTitles:nil];
    for (int nb=0; nb<3; nb++) 
    { 
        [mymenu addButtonWithTitle:@"Button Name"]; 
    }

    mymenu.cancelButtonIndex = [mymenu addButtonWithTitle: @"Cancel"];

    [mymenu showInView:self.view];
}

credit to this stackoverflow entry for the answer.

ぶ宁プ宁ぶ 2024-10-27 04:21:40
if (buttonIndex == actionSheet.cancelButtonIndex)
{
    // Handle cancel action
}

UIActionSheet 还具有可比较的属性,如 damulatoryButtonIndexfirstOtherButtonIndex

if (buttonIndex == actionSheet.cancelButtonIndex)
{
    // Handle cancel action
}

UIActionSheet also has properties like destructiveButtonIndex and firstOtherButtonIndex to compare against.

很快妥协 2024-10-27 04:21:40

添加这个

[mymenu showInView:self.parentViewController.tabBarController.view];

add this

[mymenu showInView:self.parentViewController.tabBarController.view];

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文