12

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

4 Answers4

11

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

}
Daniel
  • 357
  • 2
  • 11
  • 1
    from UX perspective, this isn't a good idea. Because the user was seeing something and is now suddenly seeing something else without any animation/feeback. The user would be dumbfounded like what?! Did that just change?!! It's best to dismiss the alertView and present a newer one – mfaani Jun 22 '17 at 15:35
  • 5
    You seriously think that UX wise it's better to dismiss and right after that present an alert view? I highly doubt that :) – Daniel Jun 24 '17 at 13:41
  • See my answer for a way to animate the change. – Jeff Jan 29 '19 at 10:35
1

It is possible to change the title and/or text AND animate the change, thusly:

[UIView transitionWithView: alertController.view
                  duration: 0.3
                   options: UIViewAnimationOptionTransitionCrossDissolve
                animations: ^(void) {
                    alertController.message = newMessage;
                }
                completion: nil];

Or in Swift:

UIView.transition(with: alertController.view,
                  duration: 0.3,
                  options: .transitionCrossDissolve,
                  animations: { alertController.message = newMessage }
)
AmitaiB
  • 1,656
  • 1
  • 19
  • 19
Jeff
  • 2,659
  • 1
  • 22
  • 41
0

Just define you alertcontroller as:

var controller:UIAlertController?

Then initialize your alert controller like this:

  controller = UIAlertController(title: "Title", message: "Yo", preferredStyle: .alert)
  self.present(controller!, animated: true, completion: nil)

Now when you get the data from server, call this:

self.controller?.title = "New Title"
Bhavuk Jain
  • 2,167
  • 1
  • 15
  • 23
0

displayMyAlertMessage(userMessage: "Keshav Gera");

func displayMyAlertMessage(userMessage: String)
    {
        let alert = UIAlertController(title: "Success", message: userMessage, preferredStyle: .alert)

        let action = UIAlertAction(title: "OK", style: .default, handler: nil)

        let imgTitle = UIImage(named:"imgTitle.png")
        let imgViewTitle = UIImageView(frame: CGRect(x: 10, y: 10, width: 30, height: 30))
        imgViewTitle.image = imgTitle

        alert.view.addSubview(imgViewTitle)
        alert.addAction(action)

        self.present(alert, animated: true, completion: nil)
    }
Keshav Gera
  • 10,807
  • 1
  • 75
  • 53