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

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

Top comments (0)

Sentry growth stunted Image

If you are wasting time trying to track down the cause of a crash, it’s time for a better solution. Get your crash rates to zero (or close to zero as possible) with less time and effort.

Try Sentry for more visibility into crashes, better workflow tools, and customizable alerts and reporting.

Switch Tools