2011-10-15 13 views
7

私は、状況に応じて異なるオプションのアクションシートを持っています。最初にボタンのタイトルの配列を作成するのに十分なボタンのタイトルがありますが、それをvarargs形式に変換する方法はわかりません。UIActionSheet varargs initメソッドに文字列の配列を送信するにはどうすればよいですか?

私はこのような何かやりたい:

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! 

しかし、それは恐ろしいだろう:私は持っていた場合、私は代わりにこのような何かを行うことが今明らかに

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

を。誰もが私を助けるためにいくつかの素晴らしい構文的な砂糖を知っていますか?

EDIT: それはそれの顔に偉大に見えた、私はaddButtonWithTitleを使用することが示唆されている、残念ながらこれは望ましくないキャンセルボタン、後に追加のボタンを置きます。

私はaddButtonWithTitle状態に関するそれぞれの文書ので、これはAppleのコードのバグであると考えている:

// 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の独自のヒューマンインターフェイスガイドラインが)他のすべてのオプション以下のキャンセルボタンを好むので、私はアップルを言うだろうねじれた。もちろん、それは本当に私を助けるものではないので、私はNSArrayとvarargsの間で変換しようとしています。これはまだやり方がわかりません。

for (Nstring *button in buttonTitles) 
    [actionSheet addButtonWithTitle:button]; 

答えて

18

次のようなこの

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"]; 
} 
[buttonTitles addObject: @"Cancel"]; 
UIActionSheet *actionSheet = [[[UIActionSheet alloc] initWithTitle: nil delegate: self cancelButtonTitle: nil destructiveButtonTitle: nil otherButtonTitles: nil] autorelease]; 

for (NSString *title in buttonTitles) { 
    [actionSheet addButtonWithTitle: title]; 
} 

[actionSheet setCancelButtonIndex: [buttonTitles count] - 1]; 
+0

私はこれがうまくいったと思っていましたが、問題がいくつか見つかりました。編集を参照してください。 –

+0

編集を確認してください。 –

+0

これは機能しました。しかし、余分なタイトルを混乱させる必要はありませんでした。 –

4

理由だけでない何かを試すことができますbuttonIndexではなくボタンのタイトルに対して:

私は、キー詳細の人が欠場と思います
- (void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex { 
    if ([[actionSheet buttonTitleAtIndex:buttonIndex] isEqualToString:kSMSButtonText]) { 
    //share via sms 
    }else if ([[actionSheet buttonTitleAtIndex:buttonIndex] isEqualToString:kEmailButtonText]) { 
    //share via mail 
    } 
} 

はinitWithTitleセレクタでcancelButtonを設定するのではなく、他のすべてのボタンの後にそれを追加しsetCancelButtonIndexを呼び出すことで、キャンセルボタンの外観を指定している:.

+0

私はこれがうまくいったと思っていましたが、問題がいくつか見つかりました。編集を参照してください。 –

5

ヒマンシュの答えのバリエーション:

UIActionSheet * as = [[UIActionSheet alloc] initWithTitle:@"Share" 
               delegate:self 
             cancelButtonTitle:nil /* don't set Cancel title here! */ 
            destructiveButtonTitle:nil 
             otherButtonTitles:nil]; 
int cancelButtonIndex = 0; // will remain 0 if no other buttons besides "Cancel" 
if (canShareBySMS) 
{ 
    [as addButtonWithTitle:kSMSButtonText]; 
    cancelButtonIndex++; 
} 
if (canShareByMail) 
{ 
    [as addButtonWithTitle:kEmailButtonText]; 
    cancelButtonIndex++; 
} 
/* etc. */ 

// after all other buttons have been added, include Cancel 
[as addButtonWithTitle:@"Cancel"]; 
[as setCancelButtonIndex:cancelButtonIndex]; 

[as showInView:self.sharingViewController.view]; 
[as release]; 

注:お使いのUIActionSheetDelegate方法は、チェックすべき

0

これは私が試した方法ですが、うまくいくようです。これは、ボタンの最後に「キャンセル」ボタンの前に追加のボタンを追加する場合に有効です。

NSString * optionalButton = nil; 
if (condition4) { 
    optionalButton = @"Some Additional Option"; 
} 

UIActionSheet * actionSheet = [[UIActionSheet alloc] initWithTitle:@"Some Title" 
                  delegate:self 
               cancelButtonTitle:@"Cancel" 
              destructiveButtonTitle:nil 
               otherButtonTitles:@"Button 1",@"Button 2",@"Button 3",optionalButton, nil]; 


[actionSheet showInView:self.view]; 

(condition4)あなたの条件が満たされない場合は、ボタンリストの末尾に余分な「ゼロ」を追加しても大丈夫であるように思われます。

これをiOS7で正常にテストしました。

関連する問題