首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用NSArray指定otherButtonTitles?

使用NSArray指定otherButtonTitles?
EN

Stack Overflow用户
提问于 2009-10-22 01:10:32
回答 5查看 18.8K关注 0票数 54

UIAlertSheet的构造函数接受一个otherButtonTitles参数作为varg列表。我想从NSArray中指定其他按钮的标题。这个是可能的吗?

例如,我必须这样做:

代码语言:javascript
复制
id alert = [[UIActionSheet alloc] initWithTitle: titleString
                                  delegate: self
                                  cancelButtonTitle: cancelString
                                  destructiveButtonTitle: nil
                                  otherButtonTitles: button1Title, button2Title, nil];

但由于我是在运行时生成可用按钮的列表,所以我真的想要这样的东西:

代码语言:javascript
复制
id alert = [[UIActionSheet alloc] initWithTitle: titleString
                                       delegate: self
                              cancelButtonTitle: cancelString
                         destructiveButtonTitle: nil
                              otherButtonTitles: otherButtonTitles];

现在,我想我需要为1个项目、2个项目和3个项目分别调用initWithTitle:。如下所示:

代码语言:javascript
复制
if ( [titles count] == 1 ) {
     alert = [[UIActionSheet alloc] initWithTitle: titleString
                                         delegate: self
                                cancelButtonTitle: cancelString
                           destructiveButtonTitle: nil
                                otherButtonTitles: [titles objectAtIndex: 0], nil];
} else if ( [titles count] == 2) {
     alert = [[UIActionSheet alloc] initWithTitle: titleString
                                         delegate: self
                                cancelButtonTitle: cancelString
                           destructiveButtonTitle: nil
                                otherButtonTitles: [titles objectAtIndex: 0], [titles objectAtIndex: 1],  nil];
} else {
    // and so on
}

这有很多重复的代码,但它实际上可能是合理的,因为我最多有三个按钮。我怎样才能避免这种情况?

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1602214

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档