Vishnu M Menon
Vishnu M Menon

Reputation: 1469

Passing Multiple Parameters to an UIAlertAction in Swift

I have been trying to find a way to pass multiple parameters to a UIAlertAction. Following is my code. I want to pass the "source" string to the joinSelected action of the alert.

I am getting an error like this:

Cannot convert value of type '()' to expected argument type '((UIAlertAction) ->

fileprivate func showBetaAlert(source: String)  {
        let betaAlert =  UIAlertController.betaProgramAlert()
        let joinAction = UIAlertAction(title: "Join", style: UIAlertActionStyle.default, handler: joinSelected(alert: <#UIAlertAction#>, source: source))
        let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: cancelSelected)

        betaAlert.addAction(cancelAction)
        betaAlert.addAction(joinAction)

        present(betaAlert, animated: true, completion: nil)

    }

fileprivate func joinSelected(alert: UIAlertAction, source: String) {
        let betaAlert = UIAlertController.signUpAlert()
        let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: dismissEmailAction)
        let submitAction = UIAlertAction(title: "Submit", style: .default , handler: { [weak self] _ in
            guard let stelf = self else { return }
            let email = betaAlert.textFields![0] as UITextField
            stelf.submitAction(email: email.text!)
        })

        betaAlert.addAction(cancelAction)
        betaAlert.addAction(submitAction)

        present(betaAlert, animated: true, completion: nil)
    }

Upvotes: 1

Views: 815

Answers (1)

rmaddy
rmaddy

Reputation: 318874

Replace this line:

let joinAction = UIAlertAction(title: "Join", style: UIAlertActionStyle.default, handler: joinSelected(alert: <#UIAlertAction#>, source: source))

with:

let joinAction = UIAlertAction(title: "Join", style: UIAlertActionStyle.default, handler: { [weak self] _ in
    joinSelected(source: source)
})

And update joinSelected to:

fileprivate func joinSelected(source: String) {

Upvotes: 2

Related Questions