取消按钮和UIActionSheet的问题

问题描述:

如何确定取消按钮是否在UIActionSheet上按下?取消按钮和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. 
} 

有没有更好的设置方法?

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

另一个小问题是在最后添加取消按钮而不是开始。

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

信贷这个*条目答案。

+0

非常好,谢谢 – shannoga 2013-10-30 15:19:54

+1

UIActionSheet必须是所有iOS中最繁琐的一段代码 – bugfixr 2014-02-17 13:04:20

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

UIActionSheet也有像destructiveButtonIndexfirstOtherButtonIndex特性来比较。

+1

对不起,我不认为我正确地问了我的问题。问题是,列表中的第一个按钮返回零的buttonIndex,它被误认为是cancelButtonIndex操作。你知道我做错了什么吗?谢谢。 – iphaaw 2011-03-23 13:00:58

+0

看起来像正确的方式,以便操作系统可以做所有它想要的取消按钮(不同的分组,风格等)的特殊事情。 – eselk 2014-12-04 16:54:44

+0

这完全返回正确的取消索引值。 – tounaobun 2015-12-15 09:36:18

添加此

[MyMenu菜单showInView:self.parentViewController.tabBarController.view];