我确信这个问题必须在某个地方得到解答,但我正在努力寻找正确的答案。如何将数组的元素传递给可变参数函数?
在我的objective-c代码中,我有一个未知数字字符串的NSArray,我想将它的元素传递给一个可变初始化方法,在这种情况下,它的... UIActionSheet constructer中的'otherButtonTitles'列表。这怎么能实现?
在此先感谢
我确信这个问题必须在某个地方得到解答,但我正在努力寻找正确的答案。如何将数组的元素传递给可变参数函数?
在我的objective-c代码中,我有一个未知数字字符串的NSArray,我想将它的元素传递给一个可变初始化方法,在这种情况下,它的... UIActionSheet constructer中的'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];
}
没有一个通用的方法来做到这一点,但是对于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可以用类似的方式初始化。
我想我期待稍微更优雅一些,谢谢,虽然,工作正常。 – Elric 2010-11-14 20:26:03