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

标签 ios swift uialertaction

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

我遇到这样的错误:

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

最佳答案

替换这一行:

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

关于ios - 在 Swift 中将多个参数传递给 UIAlertAction,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50533445/

相关文章:

ios - 我如何制作一个 Sequence 来遍历从 UIView 到 View 层次结构的所有祖先( super View )到根?

swift - Swift 中的抽象类

ios - 如何在 UIAlertController 中更改 UIAlertAction 的字体大小和颜色

ios - iOS中TableviewCell View的Wrap content

ios - 无法显示跨到另一个 tableview 的 Tableview 和自定义表格单元格

android - 平板电脑上的响应时间测量应用程序 : difference in latency between HTML and native Android/iOS?

swift - 如何获取单元格之间的距离(上、下、左、右)

ios - Admob:插页式广告尚未准备好

ios - 延迟后运行的 UIAlertAction 处理程序

swift - 使用 Swift 3 在警告消息框标题中添加图像