UIActionSheet addButtonWithTitle:不按正确的顺序添加按钮 [英] UIActionSheet addButtonWithTitle: doesn't add buttons in the right order

查看:344
本文介绍了UIActionSheet addButtonWithTitle:不按正确的顺序添加按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经继承了 UIActionSheet ,在 -init 方法中,我必须单独添加按钮调用超级 init (不能传递var_args)。



现在看起来像这样: / p>

  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;但是,在这种情况下,我的具体使用没有破坏性按钮,取消按钮和四个其他按钮。当它显示时,排序全部关闭,显示为



Button1

取消

Button2

Button3



像他们只是添加到列表的结尾,这是有道理的;然而,我不希望它看起来像这样;所以我该怎么办?实际上,是否有任何方式正确子类化 UIActionSheet 并使其工作?

解决方案

您可以按正确的顺序添加它们,然后手动设置 cancelButtonIndex destructiveButtonIndex



对于您的代码示例:

  if 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;


I've subclassed UIActionSheet, and in the -init method, I have to add the buttons individually after calling the super init (can't pass a var_args).

Right now, it looks like this:

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;

However, my specific use in this case has no destructive button, a cancel button, and four other buttons. When it shows up, the ordering is all off, showing up as

Button1
Cancel
Button2
Button3

Like they were simply added to the end of the list, which makes sense; however, I don't WANT it to look like this; so what do I do? Is there, in fact, any way to subclass UIActionSheet correctly and make this work?

解决方案

You can just add them in your correct order, and then set the cancelButtonIndex and destructiveButtonIndex manually.

For your code example:

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;

这篇关于UIActionSheet addButtonWithTitle:不按正确的顺序添加按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆