在Swift中将多个参数传递给UIAlertAction

时间:2018-05-25 16:28:38

标签: ios swift uialertaction

我一直在尝试找到一种方法将多个参数传递给UIAlertAction。以下是我的代码。我想将“source”字符串传递给警报的joinSelected操作。

我收到这样的错误:

  

无法将'()'类型的值转换为预期的参数类型'((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)
    }

1 个答案:

答案 0 :(得分:2)

替换此行:

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

使用:

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

并将joinSelected更新为:

fileprivate func joinSelected(source: String) {