当使用超过 6 个自定义按钮时,UIActionSheet buttonIndex 值出现错误?
我在 iPhone (iOS 4.2) 上使用 UIActionSheet 时发现了一个奇怪的问题。考虑以下代码:
- (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,并且按钮索引会正确报告。 1 代表按钮“一”,依此类推。如果您在添加第七个按钮的行中进行注释,则操作表会生成一种表格视图,并在额外的行上添加取消按钮。如果我在这种情况下按下“一”按钮,buttonindex 变量为 0,但 cancelButtonIndex 也为 0。无法判断用户是否点击了“取消”按钮或“一”按钮。看起来不应该是这样的。有人不同意吗?感谢您的帮助。
I have discovered a strange problem when using UIActionSheet on the iPhone (iOS 4.2). Consider this code:
- (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);
}
If you start this application, the actionsheet behaves as expected. That means the cancelButtonIndex is always 0, and the button indexes are reported correctly. 1 for button "one" and so on. If you comment in the line for adding the seventh button, the actionsheet produces a sort of tableview, with the cancel button on an extra line. If I press the "one" button in this case, the buttonindex variable is 0, but so is the cancelButtonIndex. It is impossible to tell if the user has tapped the "cancel" or the "one" button. That doesn't seem like it should be this way. Does anyone disagree? Thanks for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尽管我已经将取消按钮作为操作表中的最后一个按钮并相应地设置了其索引,但我遇到了同样的问题。我的问题与“破坏性”按钮有关。经过一番调查,我对这个问题的看法如下:
在将 N 个按钮添加到操作表后,它会切换其布局,将“破坏性”按钮放在顶部,将“取消”按钮放在底部。中间是一个可滚动视图,其中包括所有其他按钮。其他来源表明这是一个表格视图。
N 为 7(纵向)和 5(横向)。 N 是 9,表示在较大的 4 英寸屏幕上的纵向方向。这些数字适用于所有按钮,包括“取消”和“破坏性”。需要明确的是,N 是切换之前按钮的最大数量。N+1 个按钮会导致 UIActionSheet 切换到可滚动视图。
最初将“取消”和“破坏性”按钮放置在操作表中的哪个位置并不重要。一旦达到限制,“破坏性”按钮将移至顶部,“取消”将移至底部。
问题是指数没有相应调整。因此,如果您最初没有将“取消”添加为最后一个按钮,将“破坏性”添加为第一个按钮,则 actionSheet:clickedButtonAtIndex: 中将报告错误的索引,如初始报告所述。
因此,如果您的操作表中要包含 N 个以上的按钮,则必须将破坏性按钮添加到操作表中,作为操作表中的第一个按钮。您必须将“取消”按钮添加为添加到操作表的最后按钮。最初构建工作表时,只需将两者保留为 nil,如另一个答案中所述。
I ran into the same issue even though I already was including the Cancel Button as the last one in the action sheet and setting its index accordingly. My problems had to do with the 'Destructive' button. After some investigation, here is my take on the problem:
After N buttons have been added to the actionsheet, it switches its layout to put the Destructive button at the top and the Cancel button at the bottom. In between is a scrollable view that includes all of the other buttons. Other sources indicate that this is a a table view.
N is 7 for Portrait orientation and 5 for Landscape orientation. N is 9 for Portrait orientation on larger, 4" screen. These numbers are for all buttons including Cancel and Destructive. To be clear, N is the largest number of buttons before the switch. N+1 buttons causes the UIActionSheet to switch to the scrollable view.
It does not matter where in the action sheet you had originally put the Cancel and Destructive buttons within the action sheet. Once the limit has been reached, the Destructive button is moved to the top and the Cancel is moved to the bottom.
The problem is that the indices are not adjusted accordingly. So, if you did not initially add the Cancel as the last button and the Destructive as the first, the wrong index will be reported in actionSheet:clickedButtonAtIndex: as the initial report stated.
So, if you are going to have more than N buttons in your action sheet you MUST add the Destructive button to the actionSheet as the first button to the action sheet. You MUST add the Cancel button as the last button added to the action sheet. When initially constructing the sheet just leave both as nil, as described in another answer.
我刚刚遇到这个问题。通过最初不设置取消按钮来解决此问题。我单独设置按钮如下:
我相信如果您使用它,破坏性按钮将使用零索引,因此其他按钮将从那里递增,否则它们将从 0 开始。
不确定我是否同意表格选项,因为上面的一定数量后,按钮默认为可滚动列表。
I've just had this problem. Solve it by not setting the cancel button initially. I set the buttons individually something like this:
I believe the zero index is used by the destructiveButton if you use it so the other buttons will increment from there, otherwise they will start from 0.
Not sure I agree with the table option since above a certain amount, the buttons default to a scrollable list.
提交有关该问题的错误。包括一个小示例项目,然后等待几个月才能收到他们的回复。
现在,您可以在 init 工程中静态设置按钮,
不会出现任何问题。
file a bug about the problem. Include a little sample project and wait for some months to hear back from them.
For now you can set up your buttons statically in the init
works without problems.
1)使用实例化动作视图
没有取消按钮或破坏性按钮(将它们设置为 nil)
2) 使用
[myActionSheet addButtonWithTitle:(NSString *)] 正常添加所有按钮代码>.
3) 如果您想要一个特殊按钮,请使用与步骤 2 相同的方法添加它,并将标题设置为任何内容(例如
@"Cancel"
);4) 现在将属性
[myActionSheet setCancelButtonIndex:]
设置为操作表上最后一个按钮的索引,该按钮是制作的取消按钮。对破坏性按钮执行相同操作。 (默认情况下为 -1,这会导致它们不显示)请注意,破坏性按钮将始终出现在顶部,取消按钮将始终出现在底部,且无法更改。
此外,您当然可以在索引 0 处添加取消/破坏性按钮,然后添加所有其他按钮。但是现在其他按钮的第一个索引是 1,而不是零。如果您有一个与警报视图按钮对应的数组,这可能会令人困惑。
1) Instantiate the actionview using
without either the cancel button or destructive button (set them to nil)
2) Add all of the buttons as normal, using
[myActionSheet addButtonWithTitle:(NSString *)]
.3) If you want a special button, add it using the same method as step 2, and set the title to whatever (
@"Cancel"
, for instance);4) Now set the property
[myActionSheet setCancelButtonIndex:]
to the index of the last button on the actionsheet, which was the cancel button made. Do the same for a destructive button. (These are -1 by default, which causes them not to be shown)Note that a destructive button will always appear on top, and a cancel button will always appear on bottom, that cannot be changed.
Also, you can certainly add the cancel/destructive button at index 0, and add all of the other buttons after that. However now the first index of your other buttons is 1, instead of zero. This can be confusing if you have an array that corresponds with the alertview buttons.
如下所示:
这意味着将破坏性按钮定义为
demulatoryButtonTitle
。避免在otherButtonTitles
中使用破坏性/取消按钮。Do like as below:
It means that define the destructive button as
destructiveButtonTitle
. AVOID using destructive/cancel button in theotherButtonTitles
.