取消按钮和UIActionSheet的问题 [英] Problems with Cancel Button and UIActionSheet

查看:114
本文介绍了取消按钮和UIActionSheet的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我的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?

推荐答案

诀窍是不使用自动取消按钮,而是自己添加。

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

此信用 stackoverflow 条目的答案。

这篇关于取消按钮和UIActionSheet的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆