Build An Alert Dialog Box With Text Input In Swift

By Eddy Chung

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, you'll love my free guide: Secrets To iOS Development. Speed up your learning curve - hundreds of students have already downloaded. Thanks for reading!
Profile Picture of Eddy Chung

Eddy Chung

I am a professional iOS developer in Silicon Valley. I teach iOS development on ZeroToAppStore.com. If you'd like to learn more about me click here or you can contact me at: eddy@zerotoappstore.com

Similar Posts