質問

UIAlertSheetのコンストラクターは、otherButtonTitlesパラメーターを変数リストとして受け取ります。代わりに、NSArrayから他のボタンタイトルを指定したいと思います。これは可能ですか?

i.e。これをしなければなりません:

id alert = [[UIActionSheet alloc] initWithTitle: titleString
                                  delegate: self
                                  cancelButtonTitle: cancelString
                                  destructiveButtonTitle: nil
                                  otherButtonTitles: button1Title, button2Title, nil];

しかし、実行時に利用可能なボタンのリストを生成しているので、本当に次のようなものが欲しいです:

id alert = [[UIActionSheet alloc] initWithTitle: titleString
                                       delegate: self
                              cancelButtonTitle: cancelString
                         destructiveButtonTitle: nil
                              otherButtonTitles: otherButtonTitles];

今、1つのアイテム、2つのアイテム、3つのアイテムに対して initWithTitle:を個別に呼び出す必要があると考えています。このように:

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
}

これは多くの重複コードですが、ボタンが3つしかないため、実際には妥当かもしれません。どうすればこれを回避できますか?

役に立ちましたか?

解決

これは1年前ですが、解決策は非常に簡単です... @Simonが示唆したように行いますが、キャンセルボタンのタイトルを指定しないので、

UIActionSheet *alert = [[UIActionSheet alloc] initWithTitle: titleString
                              delegate: self
                              cancelButtonTitle: nil
                              destructiveButtonTitle: nil
                              otherButtonTitles: nil];

ただし、通常のボタンを追加した後、次のようにキャンセルボタンを追加します。

for( NSString *title in titles)  {
    [alert addButtonWithTitle:title]; 
}

[alert addButtonWithTitle:cancelString];

ここで重要なステップは、キャンセルボタンとなるボタンを指定することです:

alert.cancelButtonIndex = [titles count];

[titles count] ではなく、 [titles count]-1 を行います。これは、 titlesのボタンのリストからキャンセルボタンを追加するためです。

また、destructiveButtonIndex(通常は [titles count]-1 ボタン)を指定して、破壊ボタン(つまり、赤いボタン)にするボタンを指定します。また、キャンセルボタンを最後のボタンにすると、iOSは他のボタンとキャンセルボタンの間に適切なスペースを追加します。

これらはすべてiOS 2.0と互換性があるため、お楽しみください。

他のヒント

UIActionSheetを初期化するときにボタンを追加する代わりに、NSArrayを通過するforループを使用してaddButtonWithTitleメソッドでボタンを追加してみてください。

UIActionSheet *alert = [[UIActionSheet alloc] initWithTitle: titleString
                              delegate: self
                              cancelButtonTitle: cancelString
                              destructiveButtonTitle: nil
                              otherButtonTitles: nil];

for( NSString *title in titles)  
    [alert addButtonWithTitle:title]; 

addButtonWithTitle:追加されたボタンのインデックスを返します。 initメソッドでcancelButtonTitleをnilに設定し、追加のボタンを追加した後、これを実行します。

actionSheet.cancelButtonIndex = [actionSheet addButtonWithTitle:@"Cancel"];
- (void)showActionSheetWithButtons:(NSArray *)buttons withTitle:(NSString *)title {

    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle: title 
                                                             delegate: self
                                                    cancelButtonTitle: nil 
                                               destructiveButtonTitle: nil 
                                                    otherButtonTitles: nil];

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

    [actionSheet addButtonWithTitle: @"Cancel"];
    [actionSheet setCancelButtonIndex: [buttons count]];
    [actionSheet showInView:self.view];
}

キャンセルボタンを追加して、次のように設定できます。

[actionSheet setCancelButtonIndex: [actionSheet addButtonWithTitle: @"Cancel"]];

これは古い投稿であることは知っていますが、私のような他の誰かがこれを理解しようとしている場合に備えて。

(これは@kokemomukeによって回答されました。これは主により詳細な説明です。また@Ephraimと@Simonに基づいています)

addButtonWithTitleの LAST エントリは、 Cancel ボタンである必要があります。使用します:

// All titles EXCLUDING Cancel button
for( NSString *title in titles)  
    [sheet addButtonWithTitle:title];


// The next two line MUST be set correctly: 
// 1. Cancel button must be added as the last entry
// 2. Index of the Cancel button must be set to the last entry

[sheet addButtonWithTitle:@"Cancel"];

sheet.cancelButtonIndex = titles.count - 1;
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top