UIActionSheet firstOtherButtonIndex设置不正确
问题描述:
我在iPad上显示UIActionSheet这样:UIActionSheet firstOtherButtonIndex设置不正确
_actionSheet = [[UIActionSheet alloc] initWithTitle:@"Choose action"
delegate:(id<UIActionSheetDelegate>)self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
if (special_case) {
_actionSheet.destructiveButtonIndex = [_actionSheet addButtonWithTitle:@"Discard Action"];
}
[_actionSheet addButtonWithTitle:@"Action One"];
[_actionSheet addButtonWithTitle:@"Action Two"];
_actionSheet.cancelButtonIndex = [_actionSheet addButtonWithTitle:@"Cancel"];
[_actionSheet showFromBarButtonItem:self.myActionButton animated:YES];
虽然两者cancelButtonIndex
和destructiveButtonIndex
做工精细,奇怪的是,在actionSheet:didDismissWithButtonIndex:我越来越firstOtherButtonIndex
集到-1
,这显然是错误的,因为我的Action One
按钮的索引为1
,而Action Two
的索引为2
。
显然,上述的整点是只只在特殊情况下显示的破坏性按钮(否则我会通过在init调用所有标题,也许事情会一直甜蜜)。
我错过了什么?
答
firstOtherButtonIndex
大概只设置在initWithTitle
方法中。如果您手动添加按钮,则必须手动进行设置。该问题也可能是由于添加其他按钮后添加了取消按钮。通常,破坏性和取消按钮的索引低于firstOtherButtonIndex
。
那么简单的方法呢?
_actionSheet = [[UIActionSheet alloc] initWithTitle:@"Choose action"
delegate:(id)self
cancelButtonTitle:@"Cancel"
destructiveButtonTitle:(special_case ? @"Discard" : nil)
otherButtonTitles:@"Action One", @"Action Two", nil];
权,我想'firstOtherButtonIndex'不应该手动设置。至于我添加按钮的顺序,我明白这是HIG兼容的方式(首先是破坏性的,其他的按钮然后是最后的取消)。是的,破坏性和取消的索引确实较低(-1和0),所以它们与我添加按钮的顺序不匹配。 – lucianf 2012-07-23 09:44:12
警报表应该从索引中知道哪个按钮被取消,并将其显示为最后一个按钮。不过,我认为API并不完美,并且不会假设动态添加了破坏性和取消按钮。它们应该始终在构造函数中指定。 – Sulthan 2012-07-23 10:05:10
谢谢,我会记住这一点。显然,上述的“简单的方法”最有意义的在这种情况下(我只用了一切动态完成的,因为我也有一个可变数目otherButtons的)。 – lucianf 2012-07-23 12:11:22