objective-c - 如何将数组的元素传递给可变参数函数?

标签 objective-c nsarray variadic-functions

我确定这一定已经在某个地方得到了回答,但我正在努力寻找答案的正确搜索词。

在我的 objective-c 代码中,我有一个未知数字字符串的 NSArray,我想将其元素传递给可变初始化方法,在这种情况下,它是 UIActionSheet 构造函数中的“otherButtonTitles”列表。如何实现?

提前致谢

最佳答案

我认为您需要将数组的第一个元素传递给构造函数,然后使用 addButtonWithTitle 方法循环遍历其余元素并添加它们:

UIActionSheet *mySheet = [[UIActionSheet alloc] initWithTitle:title delegate:delegate cancelButtonTitle:cancelButtonTitle destructiveButtonTitle:destructiveButtonTitle otherButtonTitles:[myOtherButtons objectAtIndex:0],nil];

NSMutableArray *otherbuttons = myOtherButtons;
[otherButtons removeObjectAtIndex:0];

NSEnumerator *enumerator = [otherButtons objectEnumerator];
id anObject;

while (title = [enumerator nextObject]) {
    [mySheet addButtonWithTitle:title];
}

关于objective-c - 如何将数组的元素传递给可变参数函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4179118/

相关文章:

iphone - 如果 NSDictionary 适用于成对值,那么什么适用于三重值?

ios - 如何排序 NSArray 字母顺序?

C++ 可变参数函数指针

c++ - C/C++ va_arg - 有没有办法跳过一个参数?

c# - 是否可以分解一个数组,以便可以将其元素传递给带有 params 关键字的方法?

objective-c - OpenGL ES 不会绘制

iphone - 从 SQLite 读取 - FMDB - 初学者

ios - 使用 NSPredicate 过滤数组数组

ios - 连接到本地 wifi 网络的设备列表

ios - 从 iOS 中的路径中删除点/双点