110

UIActionSheetのボタンタイトルに使用する文字列の配列があります。残念ながら、メソッド呼び出しのotherButtonTitles:引数は、配列ではなく、文字列の可変長リストを取ります。

では、どうすればこれらのタイトルをUIActionSheetに渡すことができますか?私が提案した回避策は、otherButtonTitles:にnilを渡してから、addButtonWithTitle:を使用してボタンのタイトルを個別に指定することです。ただし、これには、[キャンセル]ボタンをUIActionSheetの最後ではなく最初の位置に移動するという問題があります。最後になりたいです。

1)文字列の可変リストの代わりに配列を渡す方法、または2)キャンセルボタンをUIActionSheetの下部に移動する方法はありますか?

ありがとう。

4

4 に答える 4

249

私はこれを機能させました(必要なのは、通常のボタンで大丈夫で、後に追加するだけです:

NSArray *array = @[@"1st Button",@"2nd Button",@"3rd Button",@"4th Button"];

    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Title Here"
                                                             delegate:self
                                                    cancelButtonTitle:nil
                                               destructiveButtonTitle:nil
                                                    otherButtonTitles:nil];

    // ObjC Fast Enumeration
    for (NSString *title in array) {
        [actionSheet addButtonWithTitle:title];
    }

    actionSheet.cancelButtonIndex = [actionSheet addButtonWithTitle:@"Cancel"];

    [actionSheet showInView:self.view];
于 2010-03-05T04:51:14.157 に答える
78

ちょっとした注意:[actionSheet addButtonWithTitle:]はそのボタンのインデックスを返すので、安全で「クリーン」にするには、次のようにします。

actionSheet.cancelButtonIndex = [actionSheet addButtonWithTitle:@"Cancel"];
于 2010-06-23T22:31:40.730 に答える
3

ジャバとニックの答えを取り、それらをもう少し拡張します。このソリューションに破壊ボタンを組み込むには:

// Create action sheet
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:title
                                                         delegate:self
                                                cancelButtonTitle:nil
                                           destructiveButtonTitle:nil
                                                otherButtonTitles:nil];
// Action Buttons
for (NSString *actionName in actionNames){
    [actionSheet addButtonWithTitle: actionName];
}

// Destruction Button
if (destructiveName.length > 0){
    [actionSheet setDestructiveButtonIndex:[actionSheet addButtonWithTitle: destructiveName]];
}

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

// Present Action Sheet
[actionSheet showInView: self.view];
于 2014-05-04T18:10:08.933 に答える
2

応答には迅速なバージョンがあります:

//array with button titles
private var values = ["Value 1", "Value 2", "Value 3"]

//create action sheet
let actionSheet = UIActionSheet(title: nil, delegate: self, cancelButtonTitle: nil, destructiveButtonTitle: nil)
//for each value in array
for value in values{
    //add a button
    actionSheet.addButtonWithTitle(value as String)
}
//display action sheet
actionSheet.showInView(self.view)

値を選択するには、ViewControllerにデリゲートを追加します。

class MyViewController: UIViewController, UIActionSheetDelegate

そして、メソッド「clickedButtonAtIndex」を実装します

func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int) {
    let selectedValue : String = values[buttonIndex]
}
于 2016-05-11T07:56:13.567 に答える