Objective-c - 将变量传递给可变长度方法

qua*_*ano 4 objective-c variable-length

我有一个包含项目的数组,我想将它们传递给一个可变长度的方法.你是怎样做的?

即,我有这个(例如):

NSArray *array = [NSArray arrayWithObjects:@"1", @"2", @"3", nil];

[[UIAlertView alloc] initWithTitle:@"title" message:@"message" delegate:nil cancelButtonTitle:[array objectAtIndex:0] otherButtonTitles:[array objectAtIndex:1], [array objectAtIndex:2], nil];
Run Code Online (Sandbox Code Playgroud)

但是想象一下,数组可能有一个可变长度的项目,所以你不能像这样硬编码.

mip*_*adi 16

otherButtonTitles参数的文档-[UIAlertView initWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles:]说明:

使用此参数等效于调用addButtonWithTitle:使用此标题添加更多按钮.

你试过这个:

NSArray *array = [NSArray arrayWithObjects:@"1", @"2", @"3", nil];
UIAlertView *view = [[UIAlertView alloc] initWithTitle:@"title" message:@"message" delegate:nil cancelButtonTitle:@"cancel" otherButtonTitles:nil];
for (NSString *s in array) {
    [view addButtonWithTitle:s];
}
Run Code Online (Sandbox Code Playgroud)