使用超过6个自定义button时UIActionSheet buttonIndex值错误?

在iPhone上使用UIActionSheet(iOS 4.2)时,我发现了一个奇怪的问题。 考虑这个代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [self.window addSubview:viewController.view]; [self.window makeKeyAndVisible]; UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"TestSheet" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles: nil]; [actionSheet addButtonWithTitle:@"one"]; [actionSheet addButtonWithTitle:@"two"]; [actionSheet addButtonWithTitle:@"three"]; [actionSheet addButtonWithTitle:@"four"]; [actionSheet addButtonWithTitle:@"five"]; [actionSheet addButtonWithTitle:@"six"]; //uncomment next line to see the problem in action //[actionSheet addButtonWithTitle:@"seven"]; [actionSheet showInView:window]; [actionSheet release]; return YES; } - (void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { NSLog(@"buttonIndex: %d, cancelButtonIndex: %d, firstOtherButtonIndex: %d", buttonIndex, actionSheet.cancelButtonIndex, actionSheet.firstOtherButtonIndex); } 

如果您启动此应用程序,则操作表的行为与预期相同。 这意味着cancelButtonIndex始终为0,并且button索引正确报告。 1为“one”button等。 如果您在添加第七个button的行中注释,则操作表将生成一种tableview,并在另外一行上显示cancelbutton。 如果在这种情况下按下“one”button,buttonindexvariables是0,但是cancelButtonIndex也是。 无法判断用户是否点击了“取消”或“一个”button。 这似乎不应该是这样的。 有没有人不同意? 谢谢你的帮助。

我遇到同样的问题,即使我已经包括取消button作为行动表中的最后一个,并相应地设置其索引。 我的问题与“破坏性”button有关。 经过一番调查,这里是我的问题:

  • 将N个button添加到操作表后,它将切换其布局,将“破坏性”button置于顶部,“取消”button置于底部。 中间是包含所有其他button的可滚动视图。 其他来源表明,这是一个表格视图。

  • 对于纵向取景,N是7,横向取景是5。 对于所有button,包括“取消”和“破坏”,N是9,对于大的4“屏幕上的纵向,要清楚,N是开关之前最多的button数量。可滚动视图。

  • 在操作表中您最初放置操作表中的“取消”和“破坏性”button的位置并不重要。 一旦达到限制,破坏性button移动到顶部,取消移动到底部。

  • 问题是这些指数没有相应调整。 所以,如果最初没有添加Cancel作为最后一个button,并且Destructive作为第一个,那么错误的索引将会在actionSheet中报告:clickedButtonAtIndex:作为初始报告。

  • 所以,如果你的行动表中有超过N个button,你必须在行动表中添加破坏性button作为第一个button。 您必须添加取消button作为添加到操作表的最后一个button。 当最初构build表单时,只需将两者都留为零,如另一个答案中所述。

我刚刚有这个问题。 通过最初不设置取消button来解决这个问题。 我个别设置button是这样的:

  for(int index = 0; index < buttonTotal; index++) { [actionSheet addButtonWithTitle:[NSString stringWithFormat:buttonText, [buttonItems objectAtIndex: index]]]; } [actionSheet addButtonWithTitle:@"Cancel"]; actionSheet.cancelButtonIndex = actionSheet.numberOfButtons; 

我相信,如果你使用零指标,那么破坏性的button就会使用零指标,所以其他button将从那里增加,否则它们将从0开始。

不确定我同意表格选项,因为超过一定的数量,button默认为可滚动列表。

提出有关问题的错误 。 包括一个小样本项目,并等待几个月,从他们那里听到。

现在,您可以在init中静态设置button

 UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"TestSheet" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles: @"one", @"two", @"three", @"four", @"five", @"six", @"seven", nil]; 

工作没有问题。

1)使用实例化actionview

[[UIActionSheet alloc] InitWithTitle:delegate:cancelButtonTitle:destructiveButtonTitle:OtherButtonTitles:]

没有取消button或破坏性button(将它们设置为零)

2)添加所有的button,使用[myActionSheet addButtonWithTitle:(NSString *)]

3)如果你想要一个特殊的button,使用与步骤2相同的方法添加它,并将标题设置为任意值(例如@"Cancel" )。

4)现在将属性[myActionSheet setCancelButtonIndex:]设置为操作表上​​最后一个button的索引,即取消button。 做一个破坏性的button相同。 (默认情况下这是-1,导致它们不被显示)

  • 请注意,一个破坏性的button将始终出现在顶部,取消button将始终出现在底部,这是不能改变。

  • 另外,您可以在索引0处添加取消/破坏性button,然后添加所有其他button。 但是现在,其他button的第一个索引是1,而不是零。 如果您有一个与alertviewbutton相对应的数组,可能会造成混淆。

做如下:

 UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"TestSheet" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Delete" otherButtonTitles: @"one", @"two", @"three", @"four", @"five", @"six", @"seven", @"eight", nil]; 

这意味着将破坏性button定义为破坏性button。 AVOID在其他button中使用破坏性/取消button。