Create UIActionSheet 'otherButtons' by passing in array, not varlist

前端 未结 4 812
太阳男子
太阳男子 2020-12-22 22:20

I have an array of strings that I want to use for button titles on a UIActionSheet. Unfortunately, the otherButtonTitles: argument in the method invocation takes a variable

4条回答
  •  别那么骄傲
    2020-12-22 22:48

    There is the swift version for the response :

    //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)
    

    To get value selected, add delegate to your ViewController :

    class MyViewController: UIViewController, UIActionSheetDelegate
    

    And implement the method "clickedButtonAtIndex"

    func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int) {
        let selectedValue : String = values[buttonIndex]
    }
    

提交回复
热议问题