Have any Question?

You can ask below or enter what you are looking for!

Display alert in iOS using UIAlertView

UIAlertController can be used to display alert messages or action sheets, in this tutorial we will be using it to display alert message.

UIAlertController inherits from UIViewController and is designed to be used as-is so you cannot subclass it or modify any of its subviews.
Add a button to your view controller and hook it to the following method and see what happens when you tap the button

    @IBAction func buttonHandler(_ sender: UIButton) {
        // create an instance of UIAlertController with title and message and style
        // the style could be alert for alert message or actioSnheet
        let alert = UIAlertController(title: "Hello", message: "Did you know that UIAlertController can have only one action with cancel style?", preferredStyle: .alert)
        // create action with title "Yes" and style default
        let actionYes = UIAlertAction(title: "Yes", style: .default) { (yes) in
            print("i choose YES")
        // create action with title "No" and style cancel
        let actionNo = UIAlertAction(title: "No", style: .cancel) { (no) in
            print("i choose no")
        // add the two actions to the alert object
        //present the alert
        self.present(alert, animated: true, completion: nil)

Leave a Reply

Your email address will not be published. Required fields are marked *