2009-08-11 14 views
9

UIActionSheetをサブクラス化し、-initメソッドでは、init(var_argsを渡すことはできません)を呼び出した後にボタンを個別に追加する必要があります。UIActionSheet addButtonWithTitle:正しい順序でボタンを追加しない

今、それは次のようになります。

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; 

ただし、この場合には、私の具体的な使用には破壊的なボタン、キャンセルボタン、および他の4つのボタンがありません。それが現れた場合、順序は、彼らは単に理にかなって、リストの末尾に追加された同様
Button2の
ボタン3

をキャンセルボタン1

として現れて、すべてをオフになっています。しかし、私はこのように見えません。私は何をしますか?実際にUIActionSheetを正しくサブクラス化してこの作業を行う方法はありますか?

答えて

21

正しい順序で追加した後、cancelButtonIndexdestructiveButtonIndexを手動で設定することができます。あなたのコードの例

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

ああ、それは簡単です。私はそれらが読み取り専用だと思った –

+4

良い答えが、実際には不要です。 addButtonWithTitle:追加されたインデックスも返します。 –

8

Aviadベンドブの答えは、しかし、ボタンのインデックスカウンタを破壊し、キャンセルのインデックスのためのインデックスを設定する必要がない、正しいです。 addButtonWithTitle:この方法は、私たちがそうのようにすぐにその値を使用することができ、新たに使用されるボタンのインデックスを返します。

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

あなたの破壊ボタンが正しい場所にないと思います。それは一番上にあるはずです。 – lhunath

3

以前の答えは破壊的なボタンはに従っていない下、に配置することが原因HIGを使用しており、ユーザにとっても非常に混乱しています。破壊的なボタンは上部に、キャンセルは下部に、その他は途中にあるべきです。

次の順序正しく:

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]; 

が実装(ブロック・ベースのAPIでUIActionSheetラッパー)のためhttps://github.com/Lyndir/Pearl/blob/master/Pearl-UIKit/PearlSheet.mを参照してください。

関連する問題