构造一个以nil结尾的NSString列表作为NSString*

Sha*_*ram 5 objective-c ios

SDK中有许多方法要求输入字符串列表,以nil结尾,例如,在UIActionSheet中:

- (id)initWithTitle:(NSString *)title delegate:(id < UIActionSheetDelegate >)delegate cancelButtonTitle:(NSString *)cancelButtonTitle destructiveButtonTitle:(NSString *)destructiveButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ...
Run Code Online (Sandbox Code Playgroud)

在这种情况下,'otherButtonTitles'是以nil结尾的NSStrings列表.我想要做的是使用NSStrings的构造NSMutableArray调用此方法,因为我想动态创建和排序参数.我该怎么做?在这种情况下,我不确定如何创建一个nil终止指向NSStrings的指针,如果传入它甚至可以工作.我是否必须手动为其分配内存并将其释放?

ken*_*ytm 9

无法将任何数组转换为可变参数列表.

但是,对于UIActionSheet,您可以在创建工作表后使用添加其他按钮标题-addButtonWithTitle:

UIActionSheet* sheet = [[UIActionSheet alloc] initWithTitle:...
                                                        /*etc*/
                                          otherButtonTitles:nil];
for (NSString* otherButtonTitle in otherButtonTitlesArray)
{
   [sheet addButtonWithTitle:otherButtonTitle];
}
Run Code Online (Sandbox Code Playgroud)

  • 如果您在UIActionSheet上设置取消按钮,这不是一个好的解决方案.当您初始化工作表然后添加带有上述循环的`otherButtons`时,出于某种原因,您的取消按钮将最终位于它们之上. (4认同)

mtw*_*ner 5

我还需要制作动态行动表.所以我做了一个大部分空的动作表.添加了我的按钮.然后添加"取消"按钮并将其标记为取消.

sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:homeName, nil];    
[sheet addButtonWithTitle:otherButton1];
[sheet addButtonWithTitle:otherButton2];
[sheet addButtonWithTitle:@"Cancel"];
[sheet setCancelButtonIndex:[sheet numberOfButtons] - 1];
sheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
[sheet showInView:self.view];
[sheet release];
Run Code Online (Sandbox Code Playgroud)