首页
学习
活动
专区
工具
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

回答 5

Stack Overflow用户

回答已采纳

发布于 2012-11-29 06:05:38

这已经有一年的历史了,但解决方案很简单……按照@Simon的建议执行操作,但不要指定cancel按钮标题,因此:

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

但在添加普通按钮后,添加取消按钮,如下所示:

代码语言:javascript
复制
for( NSString *title in titles)  {
    [alert addButtonWithTitle:title]; 
}

[alert addButtonWithTitle:cancelString];

现在,关键步骤是指定哪个按钮是cancel按钮,如下所示:

代码语言:javascript
复制
alert.cancelButtonIndex = [titles count];

我们执行[titles count]而不是[titles count] - 1,因为我们从titles的按钮列表中添加了cancel按钮作为额外按钮。

现在,您还可以通过指定destructiveButtonIndex (通常是[titles count] - 1按钮)来指定您希望哪个按钮成为破坏性按钮(即红色按钮)。此外,如果您将cancel按钮保留为最后一个按钮,iOS将在其他按钮和cancel按钮之间添加适当的间距。

所有这些都与iOS 2.0兼容,所以请尽情享受吧。

票数 72
EN

Stack Overflow用户

发布于 2009-10-22 01:23:30

与其在初始化UIActionSheet时添加按钮,不如尝试使用addButtonWithTitle方法添加它们,方法是使用遍历NSArray的for循环。

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

for( NSString *title in titles)  
    [alert addButtonWithTitle:title]; 
票数 52
EN

Stack Overflow用户

发布于 2013-08-24 13:21:15

addButtonWithTitle:返回添加的按钮的索引。在init方法中将cancelButtonTitle设置为nil,并在添加其他按钮后运行以下命令:

代码语言:javascript
复制
actionSheet.cancelButtonIndex = [actionSheet addButtonWithTitle:@"Cancel"];
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1602214

复制
相关文章

相似问题

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