Build An Alert Dialog Box With Text Input In Swift

Building an alert dialog box with text input in Swift is a common problem for iOS developers.

In this tutorial, we’ll build one together. The final result will look something like this:

Build An Alert In Swift

First we need to build an alert in Swift. To show an alert to your view when it opens, add to your viewDidAppear function:

override func viewDidAppear(_ animated: Bool) {
    let alert = UIAlertController(title: "Alert Title", message: "Alert Message", preferredStyle: .alert)
    self.present(alert, animated: true, completion: nil)
}

When you run your application you should get something like this:

alert-in-ios

Adding The Text Input Field

To add the text input field into our app, we’ll call the function addTextfield

alert.addTextField { (textField) in
    textField.placeholder = "Default placeholder text"
}

It should now look like this:

alert-in-ios_1

Let’s add a way for our user to submit text and capture that text.

alert.addAction(UIAlertAction(title: "Submit", style: .default, handler: { [weak alert] (_) in
    guard let textField = alert?.textFields?[0], let userText = textField.text else { return }
    print("User text: \(userText)")
}))

All together your code should now look like this:

class ViewController: UIViewController {
    override func viewDidAppear(_ animated: Bool) {
        let alert = UIAlertController(title: "Alert Title", message: "Alert Message", preferredStyle: .alert)
        alert.addTextField { (textField) in
            textField.placeholder = "Default placeholder text"
        }

        alert.addAction(UIAlertAction(title: "Submit", style: .default, handler: { [weak alert] (_) in
            guard let textField = alert?.textFields?[0], let userText = textField.text else { return }
            print("User text: \(userText)")
        }))

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

alert-in-ios_2

Multiple Text Input Fields

To add more text fields, we can simply call addTextField multiple times. Here’s a code example:

class ViewController: UIViewController {
    override func viewDidAppear(_ animated: Bool) {
        let alert = UIAlertController(title: "Alert Title", message: "Alert Message", preferredStyle: .alert)
        alert.addTextField { (textField) in
            textField.placeholder = "Default placeholder text"
        }

        alert.addTextField { (textField) in
            textField.placeholder = "Second textfield placeholder text"
        }

        alert.addAction(UIAlertAction(title: "Submit", style: .default, handler: { [weak alert] (_) in
            if let textField = alert?.textFields?[0], let userText = textField.text {
                print("User text: \(userText)")
            }

            if let textField = alert?.textFields?[1], let userText = textField.text {
                print("User text 2: \(userText)")
            }
        }))

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

And this is what it looks like:

alert-in-ios_3

If you liked this post and want to learn more, check out The Complete iOS Developer Bootcamp. Speed up your learning curve - hundreds of students have already joined. Thanks for reading!

Eddy Chung

I teach iOS development on ZeroToAppStore.com.

Similar Posts