Как я могу отправить массив строк в метод инициализации varargs UIActionSheet?

У меня есть лист действий с параметрами, которые меняются в зависимости от обстоятельств. Есть достаточно разных заголовков кнопок, и я хотел бы сначала создать массив этих заголовков кнопок, но я не могу понять, как преобразовать их в формат varargs.

Я хочу сделать что-то вроде этого:

NSMutableArray *buttonTitles = [NSMutableArray array];
if (condition1) {
    [buttonTitles addObject: @"Do action 1"];
}
if (condition2) {
    [buttonTitles addObject: @"Do action 2"];
}
if (condition3) {
    [buttonTitles addObject: @"Do action 3"];
}
if (condition4) {
    [buttonTitles addObject: @"Do action 4"];
}
UIActionSheet *actionSheet = [[[UIActionSheet alloc] initWithTitle: nil delegate: self cancelButtonTitle: @"Cancel" destructiveButtonTitle: nil otherButtonTitles: buttonTitles] autorelease];

Теперь очевидно, что если бы мне пришлось, я мог бы сделать что-то вроде этого:

UIActionSheet *actionSheet = nil;
if (condition1 && condition2 && condition3 && condition4) {
    actionSheet = [[[UIActionSheet alloc] initWithTitle: nil delegate: self cancelButtonTitle: @"Cancel" destructiveButtonTitle: nil otherButtonTitles: @"Do action1", @"Do action2", @"Do action3", @"Do action 4", nil] autorelease];    
} else if (condition1 && condition2 && condition3 && !condition4) {
    actionSheet = [[[UIActionSheet alloc] initWithTitle: nil delegate: self cancelButtonTitle: @"Cancel" destructiveButtonTitle: nil otherButtonTitles: @"Do action1", @"Do action2", @"Do action3", nil] autorelease];    
} 
// else ...
// about 14 other cases!

Но это было бы ужасно. Кто-нибудь знает какой-нибудь приятный синтаксический сахар, который может мне помочь?

EDIT: Было предложено использовать addButtonWithTitle , что на первый взгляд выглядит великолепно, но, к сожалению, это помещает дополнительные кнопки после кнопки отмены, что нежелательно.

Я считаю, что это ошибка в коде Apple, поскольку в их документации на addButtonWithTitle говорится:

// adds a button with the title. returns the index (0 based) of where it was added. buttons are displayed in the order added except for the
// destructive and cancel button which will be positioned based on HI requirements. buttons cannot be customized.

В требованиях HI (собственные рекомендации Apple по человеческому интерфейсу) кнопка отмены находится под всеми остальными параметрами, поэтому я бы сказал Apple облажалась. Конечно, это мне не очень помогает, поэтому я вернулся к попытке конвертировать между NSArray и varargs, что я до сих пор не знаю, как это сделать.

7
задан Micah Hainline 17 October 2011 в 23:23
поделиться