Objective c 如何将数组的元素传递给变量函数?
我肯定这个问题已经有答案了,但我正在努力寻找正确的搜索词 在我的objective-c代码中,我有一个未知数字字符串的NSArray,我想将它的元素传递给一个可变的init方法,在这种情况下,它是。。。UIActionSheet构造函数中的“OtherButtonTiles”列表。如何做到这一点Objective c 如何将数组的元素传递给变量函数?,objective-c,nsarray,variadic-functions,Objective C,Nsarray,Variadic Functions,我肯定这个问题已经有答案了,但我正在努力寻找正确的搜索词 在我的objective-c代码中,我有一个未知数字字符串的NSArray,我想将它的元素传递给一个可变的init方法,在这种情况下,它是。。。UIActionSheet构造函数中的“OtherButtonTiles”列表。如何做到这一点 提前感谢我认为您需要将数组的第一个元素传递给构造函数,然后使用addButtonWithTitle方法循环其余元素并添加它们: UIActionSheet *mySheet = [[UIActionSh
提前感谢我认为您需要将数组的第一个元素传递给构造函数,然后使用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];
}
没有一种通用的方法可以做到这一点,但对于UIActionSheet,您不需要使用该构造函数
UIActionSheet *sheet = [[UIActionSheet alloc] init];
// set properties
sheet.title = @"Title!";
// add buttons
for (NSString *buttonTitle in otherButtonTitles) {
[sheet addButtonWithTitle:buttonTitle];
}
sheet.cancelButtonIndex =
[sheet addButtonWithTitle:@"Cancel"];
UIAlertView也可以用类似的方式初始化。我想我期待的是更优雅一点的东西,不过谢谢,效果很好。