Konstruktor UIAlertSheet przyjmuje parametr otherButtonTitles jako listę varg. Chciałbym podać inne tytuły przycisków z NSArray. czy to możliwe?Użyj NSArray, aby określić inneButtonTitles?
czyli muszę to zrobić:
id alert = [[UIActionSheet alloc] initWithTitle: titleString
delegate: self
cancelButtonTitle: cancelString
destructiveButtonTitle: nil
otherButtonTitles: button1Title, button2Title, nil];
Ale ponieważ jestem generowania listy dostępnych przycisków w czasie wykonywania, naprawdę chcę coś takiego:
id alert = [[UIActionSheet alloc] initWithTitle: titleString
delegate: self
cancelButtonTitle: cancelString
destructiveButtonTitle: nil
otherButtonTitles: otherButtonTitles];
Teraz ja "Myślę, że muszę mieć osobne połączenie z initWithTitle:
dla 1 przedmiotu, 2 przedmiotów i 3 przedmiotów. Tak:
if ([titles count] == 1) {
alert = [[UIActionSheet alloc] initWithTitle: titleString
delegate: self
cancelButtonTitle: cancelString
destructiveButtonTitle: nil
otherButtonTitles: [titles objectAtIndex: 0], nil];
} else if ([titles count] == 2) {
alert = [[UIActionSheet alloc] initWithTitle: titleString
delegate: self
cancelButtonTitle: cancelString
destructiveButtonTitle: nil
otherButtonTitles: [titles objectAtIndex: 0], [titles objectAtIndex: 1], nil];
} else {
// and so on
}
To dużo duplikatu kodu, ale może to być rzeczywiście uzasadnione, ponieważ mam co najwyżej trzech przycisków. Jak mogę tego uniknąć?
Nie wiem, dlaczego to zadziałało, ale musiałem zrobić "[tytuły się liczą] - 1", żeby zadziałał dla mnie, iOS 7. – Micah
[numer alarmu] Przyciski -1 to inny sposób ustawiania indeksu przycisku anulowania – Keith
Drobny uwaga: używając nazw ustalonych tutaj, myślę, że 'sheet.cancelButtonIndex' powinno być' alert.cancelButtonIndex', yes? – Matt