Objective-C-将变量传递给可变长度方法 [英] Objective-c - Passing in variables to a variable-length method

查看:79
本文介绍了Objective-C-将变量传递给可变长度方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've got an array with items, and I want to pass these in to a variable-length method. How do you do that?

也就是说,我有这个(例如):

I.e., I've got this (for example):

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];

但是请想象一下,数组可能具有可变长度的项目,因此您不能像这样对它进行硬编码.

But imagine that array could have a variable length of items, so you cant hardcode it like this.

推荐答案

-[UIAlertView initWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles:]otherButtonTitles参数的文档指出:

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

Using this argument is equivalent to invoking addButtonWithTitle: with this title to add more buttons.

您尝试过吗:

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];
}

这篇关于Objective-C-将变量传递给可变长度方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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