Passing functions as parameters in Swift

前端 未结 4 1851
暖寄归人
暖寄归人 2021-02-05 00:17

I have the following function working as I expect, in iOS 8:

func showConfirmBox(msg:String, title:String,
    firstBtnStr:String,
    secondBtnStr:String,
    c         


        
4条回答
  •  别跟我提以往
    2021-02-05 00:48

    I wrote this routine based on various site examples. Here is how I call the routine...

    @IBAction func buttonClick(_ sender: Any) {
        SS_Alert.createAlert(parmTitle: "Choose", parmMessage: "Please select Yes or No", parmOptions: ["Yes","No","Cancel"], parmFunctions: [testYes, testNo, nil])
    }
    
    func testYes() {
        print("yes")
    }
    
    func testNo() {
        print("no")
    }
    

    You can pass in button options and the functions to performed when the buttons are selected. Took a little time to figure out how to pass functions as parameters, but appears to work fine now. I did encounter a strange problem trying to use loops to dynamically add buttons, and finally gave up and used a switch/case. I included the loop code I tried to use, if someone can figure out what I was doing wrong let me know. Thanks.

    https://github.com/blakeguitar/iOS/blob/0e243d13cb2decd6e1dbe134a8a046c2caed3876/SS_Alert.swift

提交回复
热议问题