私は、状況に応じて異なるオプションのアクションシートを持っています。最初にボタンのタイトルの配列を作成するのに十分なボタンのタイトルがありますが、それを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];
私はこれがうまくいったと思っていましたが、問題がいくつか見つかりました。編集を参照してください。 –
編集を確認してください。 –
これは機能しました。しかし、余分なタイトルを混乱させる必要はありませんでした。 –