我已经子类化了 UIActionSheet
,并且在 -init
方法中,我必须在调用 super init
后单独添加按钮(无法传递 var_args)。
现在,它看起来像这样:
if (self = [super initWithTitle:title delegate:self cancelButtonTitle:cancel destructiveButtonTile:destroy otherButtonTitles:firstButton,nil]) {
if (firstButton) {
id buttonTitle;
va_list argList;
va_start(argList, firstButtton);
while (buttonTitle = va_arg(argList, id)) {
[self addButtonWithTitle:buttonTitle]
}
va_end(argList);
}
}
return self;
但是,我在这种情况下的具体用途是没有破坏性按钮、一个取消按钮和其他四个按钮。当它出现时,排序全部关闭,显示为
按钮1
取消
按钮2
按钮3
就像它们只是被添加到列表的末尾一样,这是有道理的;但是,我不希望它看起来像这样;那我该怎么办?事实上,是否有任何方法可以正确地子类化 UIActionSheet
并使其工作?
最佳答案
您可以按照正确的顺序添加它们,然后手动设置 cancelButtonIndex
和 destroyButtonIndex
。
对于您的代码示例:
if (self = [super initWithTitle:title delegate:self cancelButtonTitle:nil destructiveButtonTile:nil otherButtonTitles:nil]) {
if (firstButton) {
id buttonTitle;
int idx = 0;
va_list argList;
va_start(argList, firstButtton);
while (buttonTitle = va_arg(argList, id)) {
[self addButtonWithTitle:buttonTitle]
idx++;
}
va_end(argList);
[self addButtonWithTitle:cancel];
[self addButtonWithTitle:destroy];
self.cancelButtonIndex = idx++;
self.destructiveButtonIndex = idx++;
}
}
return self;
关于iphone - UIActionSheet addButtonWithTitle : doesn't add buttons in the right order,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1262205/