John511
John511

Reputation: 29

Swift UIAlertController showing custom message

I have a ViewController class thats only purpose is to display an alert message with a custom message and title that is passes in through a custom init message. This is done as soon as the view appears, in viewDidLoad. My problem however is that when it comes to this view it pops up and is stuck in this view forever, instead of just putting the view over the other. I am not sure how to fix this. Here is the code for my alertVC class

import UIKit

class AlertVC: UIViewController {

  var myMessage: String?
  var myTitle: String?

  override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
  }

  override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
  }


  override func viewDidAppear(animated: Bool){
    let alertController = UIAlertController(title: myTitle, message: myMessage, preferredStyle: .Alert)
    let OKAction = UIAlertAction(title: "OK", style: .Default) {
        (action: UIAlertAction) in print("Youve pressed OK Button")
    }
    alertController.addAction(OKAction)
    self.presentViewController(alertController, animated: true, completion: nil)
  }

  convenience init(title: String, message: String){
    self.init()
    self.myTitle = title
    self.myMessage = message
  }
}

This is the code for how I create an object for this and try and show it.

let alert = AlertVC(title: "Error", message: "error")
presentViewController(alert, animated: true, completion: nil)

Any help is appreciated, leave a comment if you need more info. Thanks!

Upvotes: 2

Views: 9938

Answers (3)

vadian
vadian

Reputation: 285079

Why not simply an extension?

extension UIViewController {

  func presentAlert(withTitle title: String, message : String) {
    let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
    let OKAction = UIAlertAction(title: "OK", style: .default) { action in
        print("You've pressed OK Button")
    }
    alertController.addAction(OKAction)
    self.present(alertController, animated: true, completion: nil)
  }
}

and call it with

presentAlert(withTitle: "Error", message: "error")

in any UIViewController class

Upvotes: 25

ZaEeM ZaFaR
ZaEeM ZaFaR

Reputation: 1536

@vadian's answer in Swift 3 with completion handler added to alert controller dismiss.

extension UIViewController {

    func presentAlertWithTitle(title: String, message : String, onDismiss: SimpleCompletionBlock? = nil)
    {
        let alertController = UIAlertController(title: title, message: message, preferredStyle: .Alert)

        let OKAction = UIAlertAction(title: "OK", style: .Default) {
            (action: UIAlertAction) in print("Youve pressed OK Button")

            onDismiss?()
        }

        alertController.addAction(OKAction)
        self.presentViewController(alertController, animated: true, completion: nil)
    }
}

Upvotes: 0

Daven
Daven

Reputation: 599

If AlertVC's only purpose is to display the UIAlertController, why don't you just present the UIAlertController on your previous ViewController?

So where you have written this:

let alert = AlertVC(title: "Error", message: "error")
presentViewController(alert, animated: true, completion: nil)

Replace it with:

let alertController = UIAlertController(title: myTitle, message: myMessage, preferredStyle: .Alert)
let OKAction = UIAlertAction(title: "OK", style: .Default) {
    (action: UIAlertAction) in print("Youve pressed OK Button")
}
alertController.addAction(OKAction)
self.presentViewController(alertController, animated: true, completion: nil)

EDIT: After reading your comment, I understand that you want to avoid typing the same code multiple times. I've done some quick research and it seems you shouldn't subclass UIAlertController, so perhaps an extension may be useful? Something like:

extension UIAlertController{
    class func customAlertController(title : String, message : String) -> UIAlertController{
        let alertController = UIAlertController(title: title, message: message, preferredStyle: .Alert)
        let OKAction = UIAlertAction(title: "OK", style: .Default) {
            (action: UIAlertAction) in print("Youve pressed OK Button")
        }
        alertController.addAction(OKAction)
        return alertController
    }
}

Then replace:

let alert = AlertVC(title: "Error", message: "error")
presentViewController(alert, animated: true, completion: nil)

with:

    let alert = UIAlertController.customAlertController("Error!", message: "error")
    self.presentViewController(alert, animated: true) {

    }

Upvotes: 0

Related Questions