DEV Community

Avelyn Hyunjeong Choi
Avelyn Hyunjeong Choi

Posted on • Edited on

How to use UIAlertController in Xcode

IOS provides two constant values:

  • .alert
  • .actionSheet two constant values

.alert

    @IBAction func alertCalled(_ sender: Any) {
        let alert = UIAlertController(title: "Delete Message", message: "Are you sure you want to delete this message?", preferredStyle: .alert)

        // two buttons
        let cancelAction = UIAlertAction(title: "No", style: .cancel)
        let yesAction = UIAlertAction(title: "Yes", style: .destructive) { _ in
            print("message deleted!")
        }

        // add action to the alert
        alert.addAction(yesAction)
        alert.addAction(cancelAction)

        self.present(alert, animated: true)
    }
Enter fullscreen mode Exit fullscreen mode

alert image

.actionSheet

    @IBAction func alertCalled(_ sender: Any) {
        let alert = UIAlertController(title: "Delete Message!", message: "Are you sure you want to delete this message?", preferredStyle: .actionSheet)

        // two buttons
        let cancelAction = UIAlertAction(title: "No", style: .cancel)
        let yesAction = UIAlertAction(title: "Yes", style: .destructive) { _ in
            print("message deleted!")
        }

        // add action to the alert
        alert.addAction(yesAction)
        alert.addAction(cancelAction)

        self.present(alert, animated: true)
    }
Enter fullscreen mode Exit fullscreen mode

actionSheet

AlertControllers accepting user input
NOTE: you CANNOT use .actionSheet with addTextField() method

    @IBAction func alertWithTextFieldCalled(_ sender: Any) {
        let alert = UIAlertController(title: "Hello", message: "What is your name?", preferredStyle: .alert)

        alert.addTextField() { textField in
            textField.placeholder = "Enter your name"
        }

        // two buttons
        let cancelAction = UIAlertAction(title: "No", style: .cancel)
        let yesAction = UIAlertAction(title: "Yes", style: .destructive) { _ in
            let textField = alert.textFields![0] as UITextField
            guard let name = textField.text else {
                return
            }
            print("Hello \(name)")
        }

        // add action to the alert
        alert.addAction(yesAction)
        alert.addAction(cancelAction)

        self.present(alert, animated: true)
    }
Enter fullscreen mode Exit fullscreen mode

user input

result

Sentry mobile image

Improving mobile performance, from slow screens to app start time

Based on our experience working with thousands of mobile developer teams, we developed a mobile monitoring maturity curve.

Read more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay