Santiago carmona gonzalez Santiago carmona gonzalez - 1 month ago 10
iOS Question

Change UIAlertController message or title during its presentation

Is posible change the title or message text of

UIAlertController
while its
ViewController
is presenting.

For example I present an alert when a user press a button with this message:

"Waiting for redeem code"

Then I use Alamofire to make a request and get the code, after I have it I want to change the message from alert without dissmising and presenting it again, for example the new message text is it:

"Your redeem code is : ########"

Updated

Here is my code:

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

Answer

this is possible of course, please check the following:

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

}