2009-08-11 10 views
9

Podklasowałem UIActionSheet, a w metodzie -init muszę osobno dodać przyciski po wywołaniu super init (nie można przekazać zmiennej var_args).UIActionSheet addButtonWithTitle: nie dodaje przycisków w odpowiedniej kolejności

Teraz wygląda to tak:

if (self = [super initWithTitle:title delegate:self cancelButtonTitle:cancel destructiveButtonTile:destroy otherButtonTitles:firstButton,nil]) { 
    if (firstButton) { 
    id buttonTitle; 
    va_list argList; 
    va_start(argList, firstButtton); 
    while (buttonTitle = va_arg(argList, id)) { 
     [self addButtonWithTitle:buttonTitle] 
    } 
    va_end(argList); 
    } 
} 
return self; 

Jednak moje szczególne zastosowanie w tym przypadku nie ma destrukcyjny kliknąć przycisk Anuluj, a cztery inne przyciski. Kiedy pojawia się, zamawianie wszystko jest wyłączony, wyświetlany jako

Przycisk1
Anuluj
button2
button3

jakby były po prostu dodaje się do końca listy, która ma sens; jednak nie chcę, żeby to wyglądało; Więc co mam zrobić? Czy rzeczywiście istnieje jakiś sposób podklasy UIActionSheet poprawnie i sprawiają, że to działa?

Odpowiedz

21

Możesz po prostu dodać je we właściwej kolejności, a następnie ręcznie ustawić cancelButtonIndex i destructiveButtonIndex.

Dla przykładu kodu:

if (self = [super initWithTitle:title delegate:self cancelButtonTitle:nil destructiveButtonTile:nil otherButtonTitles:nil]) { 
    if (firstButton) { 
    id buttonTitle; 
    int idx = 0; 
    va_list argList; 
    va_start(argList, firstButtton); 
    while (buttonTitle = va_arg(argList, id)) { 
     [self addButtonWithTitle:buttonTitle] 
     idx++; 
    } 
    va_end(argList); 
    [self addButtonWithTitle:cancel]; 
    [self addButtonWithTitle:destroy]; 
    self.cancelButtonIndex = idx++; 
    self.destructiveButtonIndex = idx++; 
    } 
} 
return self; 
+1

Ah, to ułatwia. Myślałem, że to były tylko do odczytu. –

+4

Dobra odpowiedź, ale licznik jest w rzeczywistości niepotrzebny. addButtonWithTitle: zwraca indeks, który został dodany. –

8

odpowiedź Aviad Ben Dov jest poprawne, jednak licznik indeksu przycisk nie jest potrzebne, aby ustawić wskaźnik dla niszczą i anulować indeksów. AddButtonWithTitle: metoda zwraca wskaźnik przycisku nowo używane więc możemy wykorzystać tę wartość od razu tak:

if (self = [super initWithTitle:title delegate:self cancelButtonTitle:nil destructiveButtonTile:nil otherButtonTitles:nil]) { 
    if (firstButton) { 
    id buttonTitle; 
    va_list argList; 
    va_start(argList, firstButtton); 
    while (buttonTitle = va_arg(argList, id)) { 
     [self addButtonWithTitle:buttonTitle] 
    } 
    va_end(argList); 
    self.cancelButtonIndex = [self addButtonWithTitle:cancel]; 
    self.destructiveButtonIndex = [self addButtonWithTitle:destroy]; 
    } 
} 
return self; 
+0

Myślę, że twój przycisk niszczenia nie znajduje się we właściwej lokalizacji. Powinien być na górze. – lhunath

3

wcześniejszych odpowiedziach powodować przycisk destrukcyjny być umieszczone na dole, co nie jest zgodne z HIG, a także jest bardzo mylące dla użytkownika. Destrukcyjny przycisk powinien znajdować się u góry, anulować na dole, a pozostałe na środku.

następujące rozkazy je poprawnie:

sheetView   = [[UIActionSheet alloc] initWithTitle:title delegate:self 
             cancelButtonTitle:nil destructiveButtonTitle:destructiveTitle otherButtonTitles:firstOtherTitle, nil]; 
if (otherTitlesList) { 
    for (NSString *otherTitle; (otherTitle = va_arg(otherTitlesList, id));) 
     [sheetView addButtonWithTitle:otherTitle]; 
    va_end(otherTitlesList); 
} 
if (cancelTitle) 
    sheetView.cancelButtonIndex  = [sheetView addButtonWithTitle:cancelTitle]; 

Zobacz https://github.com/Lyndir/Pearl/blob/master/Pearl-UIKit/PearlSheet.m dla implementacji (wrapper UIActionSheet z bloku API oparte).

Powiązane problemy