gpt4 book ai didi

iphone - 如何将字符串数组发送到 UIActionSheet varargs init 方法中?

转载 作者:可可西里 更新时间:2023-11-01 03:23:49 26 4
gpt4 key购买 nike

我有一个行动表,其中的选项会根据情况而有所不同。有足够多不同的按钮标题,我想首先构建这些按钮标题的数组,但我不知道如何将其转换为可变参数格式。

我想做这样的事情:

NSMutableArray *buttonTitles = [NSMutableArray array];
if (condition1) {
[buttonTitles addObject: @"Do action 1"];
}
if (condition2) {
[buttonTitles addObject: @"Do action 2"];
}
if (condition3) {
[buttonTitles addObject: @"Do action 3"];
}
if (condition4) {
[buttonTitles addObject: @"Do action 4"];
}
UIActionSheet *actionSheet = [[[UIActionSheet alloc] initWithTitle: nil delegate: self cancelButtonTitle: @"Cancel" destructiveButtonTitle: nil otherButtonTitles: buttonTitles] autorelease];

现在很明显,如果我不得不这样做,我可以改为这样做:

UIActionSheet *actionSheet = nil;
if (condition1 && condition2 && condition3 && condition4) {
actionSheet = [[[UIActionSheet alloc] initWithTitle: nil delegate: self cancelButtonTitle: @"Cancel" destructiveButtonTitle: nil otherButtonTitles: @"Do action1", @"Do action2", @"Do action3", @"Do action 4", nil] autorelease];
} else if (condition1 && condition2 && condition3 && !condition4) {
actionSheet = [[[UIActionSheet alloc] initWithTitle: nil delegate: self cancelButtonTitle: @"Cancel" destructiveButtonTitle: nil otherButtonTitles: @"Do action1", @"Do action2", @"Do action3", nil] autorelease];
}
// else ...
// about 14 other cases!

但那将是可怕的。任何人都知道一些不错的语法糖来帮助我吗?

编辑:有人建议我使用 addButtonWithTitle,从表面上看它看起来很棒,不幸的是,它将附加按钮放在取消按钮之后,这是不可取的。

我相信这是 Apple 代码的错误,因为他们在 addButtonWithTitle 上的文档指出:

// adds a button with the title. returns the index (0 based) of where it was added. buttons are displayed in the order added except for the
// destructive and cancel button which will be positioned based on HI requirements. buttons cannot be customized.

HI 要求(Apple 自己的人机界面指南)支持所有其他选项下方的取消按钮,所以我认为 Apple 搞砸了。当然,这并没有真正帮助我,所以我又开始尝试在 NSArray 和可变参数之间进行转换,但我仍然不知道该怎么做。

最佳答案

你可以试试这个

NSMutableArray *buttonTitles = [NSMutableArray array];
if (condition1) {
[buttonTitles addObject: @"Do action 1"];
}
if (condition2) {
[buttonTitles addObject: @"Do action 2"];
}
if (condition3) {
[buttonTitles addObject: @"Do action 3"];
}
if (condition4) {
[buttonTitles addObject: @"Do action 4"];
}
[buttonTitles addObject: @"Cancel"];
UIActionSheet *actionSheet = [[[UIActionSheet alloc] initWithTitle: nil delegate: self cancelButtonTitle: nil destructiveButtonTitle: nil otherButtonTitles: nil] autorelease];

for (NSString *title in buttonTitles) {
[actionSheet addButtonWithTitle: title];
}

[actionSheet setCancelButtonIndex: [buttonTitles count] - 1];

关于iphone - 如何将字符串数组发送到 UIActionSheet varargs init 方法中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7781022/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com