使用NSArray指定otherButtonTitles? [英] Use NSArray to specify otherButtonTitles?

查看:94
本文介绍了使用NSArray指定otherButtonTitles?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

UIAlertSheet's constructor takes an otherButtonTitles parameter as a varg list. I'd like to specify the other button titles from an NSArray instead. Is this possible?

即。我必须这样做:

id alert = [[UIActionSheet alloc] initWithTitle: titleString
                                  delegate: self
                                  cancelButtonTitle: cancelString
                                  destructiveButtonTitle: nil
                                  otherButtonTitles: button1Title, button2Title, nil];

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

But since I'm generating the list of available buttons at runtime, I really want something like this:

id alert = [[UIActionSheet alloc] initWithTitle: titleString
                                       delegate: self
                              cancelButtonTitle: cancelString
                         destructiveButtonTitle: nil
                              otherButtonTitles: otherButtonTitles];

现在,我想我需要单独拨打 initWithTitle:包含1个项目,2个项目和3个项目。像这样:

Right now, I'm thinking that I need to have a seperate call to initWithTitle: for 1 item, 2 items and 3 items. Like this:

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
}

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

That's a lot of duplicate code, but it might actually be reasonable since I have at most three buttons. How can I avoid this?

推荐答案

这已经有一年了,但解决方案非常简单......就像@Simon建议的那样但是不要指定取消按钮标题,所以:

This is a year old but the solution is pretty simple ... do as @Simon suggested but do not specify a cancel button title, so:

UIActionSheet *alert = [[UIActionSheet alloc] initWithTitle: titleString
                              delegate: self
                              cancelButtonTitle: nil
                              destructiveButtonTitle: nil
                              otherButtonTitles: nil];

但添加正常按钮后,添加取消按钮,如:

But after adding your normal buttons, add the cancel button, like:

for( NSString *title in titles)  {
    [alert addButtonWithTitle:title]; 
}

[alert addButtonWithTitle:cancelString];

现在关键步骤是指定哪个按钮是取消按钮,如:

Now the key step is to specify which button is the cancel button, like:

alert.cancelButtonIndex = [titles count];

我们 [titles count] 而不是 [titles count] - 1 因为我们在 titles 中的按钮列表中添加了取消按钮。

We do [titles count] and not [titles count] - 1 because we are adding the cancel button as extra from the list of buttons in titles.

您现在还可以通过指定destructiveButtonIndex(通常是 [指定]来指定您想要成为破坏性按钮的按钮(即红色按钮)。标题计数] - 1 按钮)。此外,如果您将取消按钮保留为最后一个按钮,iOS将在其他按钮和取消按钮之间添加漂亮的间距。

You now also specify which button you want to be the destructive button (ie the red button) by specifying the destructiveButtonIndex (typically that will be the [titles count] - 1 button). Also, if you keep the cancel button to be the last button, iOS will add that nice spacing between the other buttons and the cancel button.

所有这些都是iOS 2.0兼容所以享受。

All of these is iOS 2.0 compatible so enjoy.

这篇关于使用NSArray指定otherButtonTitles?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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