SDK中有许多方法要求输入字符串列表,以nil结尾,例如,在UIActionSheet中:
- (id)initWithTitle:(NSString *)title delegate:(id < UIActionSheetDelegate >)delegate cancelButtonTitle:(NSString *)cancelButtonTitle destructiveButtonTitle:(NSString *)destructiveButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ...
在这种情况下,答案 0 :(得分:9)
您无法将任何数组转换为可变参数列表。
但是,对于UIActionSheet,您可以在创建工作表后使用-addButtonWithTitle:
UIActionSheet* sheet = [[UIActionSheet alloc] initWithTitle:...
/*etc*/
otherButtonTitles:nil];
for (NSString* otherButtonTitle in otherButtonTitlesArray)
{
[sheet addButtonWithTitle:otherButtonTitle];
}
答案 1 :(得分: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];
答案 2 :(得分:1)
您确定 CAN 将NSArray转换为va_list。例如,与NSString
- (id)initWithFormat:(NSString *)format arguments:(va_list)argList
像这样:
+ (id)stringWithFormat:(NSString *)format array:(NSArray *)arguments;
{
NSRange range = NSMakeRange(0, [arguments count]);
NSMutableData *data = [NSMutableData dataWithLength:sizeof(id) * [arguments count]];
[arguments getObjects:(__unsafe_unretained id *) data.mutableBytes range:range];
return [[NSString alloc] initWithFormat:format arguments:data.mutableBytes];
}