在演示期间更改UIAlertController消息或标题

San*_*lez 6 ios swift uialertcontroller

是否可以更改UIAlertControllerViewController呈现时的标题或消息文本.

例如,当用户按下带有此消息的按钮时,我会发出警报:

"等待兑换代码"

然后我使用Alamofire发出请求并获取代码,在我拥有它之后,我想从警报中更改消息而不会有任何不知情并再次呈现它,例如新的消息文本是:

"您的兑换码是:########"

更新

这是我的代码:

@IBAction func offerAction(_ sender: UIButton) {
        var code: String = "Generating códe"
        let message: String = "El código para redimir esta oferta es: \(code)"

        let alert = UIAlertController(title: nil, message: message, preferredStyle: .alert)
        let okAction = UIAlertAction(title: "Accept", style: .default, handler: nil)
        let redirect = UIAlertAction(title: "Website", style: .default) { (_) in
            // TODO open url web
        }

        if offer.redeemOfferOnline == .yes {
            alert.addAction(redirect)
        }
        alert.addAction(okAction)
        present(alert, animated: true, completion: nil)

        offer.code.getRedeemCode(id: offer.id) { (success, data) in
            if success {
                code = data
                alert.message = message

                // TODO end the code of changing the message of alert
            }
        }
}
Run Code Online (Sandbox Code Playgroud)

Dan*_*iel 6

这当然是可能的,请检查以下内容:

class MyViewController:  UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let alertController = UIAlertController(title: "My Title", message: "My Message", preferredStyle: .alert)

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

        // Do your queries and get your new title and then set the new title
        alertController.title = "Your new title"
    }

}
Run Code Online (Sandbox Code Playgroud)

  • 您认真地认为UX明智的做法是,最好将其关闭,然后再显示警报视图?我对此表示高度怀疑:) (3认同)