How to keep Alerts with UIAlertController in Swift 5.2 with Xcode 11


In this articles, We will learn How to use UIAlertController  class means An object that displays an alert message to the user.Here we will learn iOS UI alerts in swift with example and how to use ios alerts in swift to convey important information with an example using Xcode.

UIAlertController is one among the foremost basic component of the app development and important part of iOS SDK.This article is specially written for beginner iOS developers.

UIAlertController has three step.

1- Instantiate UIAlertController with init(title:message:preferredStyle:). The title: and message: are large and little descriptive text to seem at the highest of the dialog. The preferredStyle: (UIAlertControllerStyle) are going to be either .alert or .actionSheet.

2- Configure the dialog by calling addAction(_:) on the UIAlertController as repeatedly as required . An action may be a UIAlertAction, which basically means it’s a button to seem within the dialog, along side a function to be executed when the button is tapped; to create one, call init(title:style:handler:). Possible style: values are (UIAlertActionStyle):

  • .default
  • .destructive
  • .cancel

3- Call present(_:animated:completion:) to present the UIAlertController. The dialog is automatically dismissed when the user taps any button.

Simple alert Only One Button-




   

 @IBAction func showOneButtonTapped(_ sender: UIButton) {


        // create the alert

         let alert = UIAlertController(title: "AppCodeZip", message: "pls like appcodezip fb page.", preferredStyle: UIAlertController.Style.alert)


        // add an action (button)

        alert.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: nil))


        // show the alert

        self.present(alert, animated: true, completion: nil)

    }



Alert with 2 buttons-


   @IBAction func showTwoButtonTapped(_ sender: Any) {

        let alert = UIAlertController(title: "Sign out?", message: "Are You Sure to Log Out ? ",preferredStyle: UIAlertController.Style.alert)


        alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertAction.Style.default, handler: { _ in

            //Cancel Action

        }))

        alert.addAction(UIAlertAction(title: "OK",

                                      style: UIAlertAction.Style.default,

                                      handler: {(_: UIAlertAction!) in

                                        //Sign out action

        }))

        self.present(alert, animated: true, completion: nil)

    }


Alert with more than 2 buttons-



@IBAction func showThreeButtonTapped(_ sender: Any) {

        let alert = UIAlertController(title: "AppCodeZip", message: "Alert with more than 2 buttons", preferredStyle: .alert)


        alert.addAction(UIAlertAction(title: "Default", style: .default, handler: { (_) in

            print("You've pressed default")

        }))


        alert.addAction(UIAlertAction(title: "Cancel", style: .default, handler: { (_) in

            print("You've pressed cancel")

        }))


        alert.addAction(UIAlertAction(title: "Destructive", style: .destructive, handler: { (_) in

            print("You've pressed the destructive")

        }))

        self.present(alert, animated: true, completion: nil)

    }


Simple action sheet- 



@IBAction func simpleActionSheet(_ sender: Any) {


              let alert = UIAlertController(title: "AppCodeZip", message: "Please Select an Option", preferredStyle: .actionSheet)

              alert.addAction(UIAlertAction(title: "Submit", style: .default, handler: { (_) in

                  print("User click Submit button")

              }))


              alert.addAction(UIAlertAction(title: "Edit", style: .default, handler: { (_) in

                  print("User click Edit button")

              }))


              alert.addAction(UIAlertAction(title: "Delete", style: .destructive, handler: { (_) in

                  print("User click Delete button")

              }))


              alert.addAction(UIAlertAction(title: "Dismiss", style: .cancel, handler: { (_) in

                  print("User click Dismiss button")

              }))


              self.present(alert, animated: true, completion: {

                  print("completion block")

              })

          }



Alert show with distractive button-



    @IBAction func showAlertWithDistructiveButton(_ sender: Any) {

        let alert = UIAlertController(title: "Sign out?", message: "You can always access your content by signing back in", preferredStyle: UIAlertController.Style.alert)


        alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertAction.Style.default, handler: { _ in

            //Cancel Action

        }))

        alert.addAction(UIAlertAction(title: "Sign out",

                                      style: UIAlertAction.Style.destructive,

                                      handler: {(_: UIAlertAction!) in

                                        //Sign out action

        }))

        self.present(alert, animated: true, completion: nil)

    }


Alert with UITextField-



@IBAction func showAlertWithTextField(_ sender: Any) {

        let alertController = UIAlertController(title: "Pls enter email id for subscribe", message: nil, preferredStyle: .alert)

        let confirmAction = UIAlertAction(title: "Submit", style: .default) { (_) in

            if let txtField = alertController.textFields?.first, let text = txtField.text {

         

                print("TextMsg==>" + text)

            }

        }

        let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (_) in }

        alertController.addTextField { (textField) in

            placeholder = "Please Subscribe AppCodeZip"

        }

        alertController.addAction(confirmAction)

        alertController.addAction(cancelAction)

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

    }




You can download full source code from GitHubAs always, leave us comment and share your thought about the tutorial.

I hope this blog was helpful to you. So, please share it together with your friends and colleagues using the social buttons below!












Post a Comment

0 Comments