Swift
UIAlertController
TextField
iOS Development
SwiftUI

How to add TextField to UIAlertController in Swift

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

UIAlertController can collect lightweight user input without forcing you to build a dedicated form screen. In UIKit, the standard pattern is to create an alert with style .alert, add one or more text fields, and read their values in an action handler. The important details are that text fields are supported only for alert-style controllers and that you should validate the input before acting on it.

Create an Alert and Add a Text Field

The minimal pattern looks like this:

swift
1import UIKit
2
3class ViewController: UIViewController {
4    func showNamePrompt() {
5        let alert = UIAlertController(
6            title: "Enter Name",
7            message: "Type a display name",
8            preferredStyle: .alert
9        )
10
11        alert.addTextField { textField in
12            textField.placeholder = "Name"
13            textField.autocapitalizationType = .words
14        }
15
16        let cancel = UIAlertAction(title: "Cancel", style: .cancel)
17        let save = UIAlertAction(title: "Save", style: .default) { _ in
18            let name = alert.textFields?.first?.text ?? ""
19            print("User entered: \(name)")
20        }
21
22        alert.addAction(cancel)
23        alert.addAction(save)
24
25        present(alert, animated: true)
26    }
27}

This is the basic recipe:

  • create alert controller
  • add text field
  • add actions
  • read the text field value inside the action handler

The alert owns the text field, so you usually read it through alert.textFields.

Configure the Text Field Properly

addTextField gives you a configuration block, which is the right place to set keyboard type, secure entry, autocorrection, and placeholder text.

swift
1alert.addTextField { textField in
2    textField.placeholder = "Email"
3    textField.keyboardType = .emailAddress
4    textField.autocapitalizationType = .none
5    textField.autocorrectionType = .no
6}

For passwords:

swift
1alert.addTextField { textField in
2    textField.placeholder = "Password"
3    textField.isSecureTextEntry = true
4}

Small configuration choices matter because alert-based input is intentionally compact. You want the keyboard and behavior to match the data immediately.

Add Multiple Text Fields Safely

You can call addTextField more than once when the input stays simple enough for an alert.

swift
1let alert = UIAlertController(
2    title: "Login",
3    message: "Enter your credentials",
4    preferredStyle: .alert
5)
6
7alert.addTextField { $0.placeholder = "Username" }
8alert.addTextField {
9    $0.placeholder = "Password"
10    $0.isSecureTextEntry = true
11}
12
13let login = UIAlertAction(title: "Login", style: .default) { _ in
14    let username = alert.textFields?[0].text ?? ""
15    let password = alert.textFields?[1].text ?? ""
16    print(username, password)
17}
18
19alert.addAction(UIAlertAction(title: "Cancel", style: .cancel))
20alert.addAction(login)
21present(alert, animated: true)

This works well for one or two fields. If the UI grows beyond that, a dedicated form screen is usually a better user experience than stuffing more controls into an alert.

Validate Before Accepting the Input

An alert with text fields is easy to present, but it is still your job to validate the entered data. A simple pattern is to disable the primary action until the field is non-empty.

swift
1let alert = UIAlertController(title: "Rename", message: nil, preferredStyle: .alert)
2
3alert.addTextField { textField in
4    textField.placeholder = "New title"
5}
6
7let save = UIAlertAction(title: "Save", style: .default) { _ in
8    let value = alert.textFields?.first?.text ?? ""
9    print("Saving \(value)")
10}
11save.isEnabled = false
12
13NotificationCenter.default.addObserver(
14    forName: UITextField.textDidChangeNotification,
15    object: alert.textFields?.first,
16    queue: .main
17) { _ in
18    let value = alert.textFields?.first?.text ?? ""
19    save.isEnabled = !value.trimmingCharacters(in: .whitespaces).isEmpty
20}
21
22alert.addAction(UIAlertAction(title: "Cancel", style: .cancel))
23alert.addAction(save)
24present(alert, animated: true)

This keeps the alert interaction clean and avoids empty submissions.

Know the Limitations

Text fields work only with .alert, not with .actionSheet. That is a common surprise. If you need an action sheet, use buttons only. If you need input, use alert style or present a custom view controller.

Also remember that UIAlertController is a UIKit API. In a SwiftUI app, you usually bridge to UIKit for this specific pattern or use a custom sheet instead of forcing alert-based text entry everywhere.

Common Pitfalls

  • Using .actionSheet and expecting text fields to appear.
  • Reading alert.textFields outside the action flow without understanding alert ownership and timing.
  • Forgetting to validate empty or malformed input before processing it.
  • Putting too many fields into one alert instead of moving to a proper form screen.
  • Neglecting text field configuration such as keyboard type or secure entry for the data being collected.

Summary

  • Add text fields to UIAlertController only when using .alert style.
  • Configure each field inside addTextField.
  • Read the values from alert.textFields in the action handler.
  • Keep alert-based input small and validate it before accepting it.
  • When the interaction grows beyond simple prompts, switch to a dedicated screen instead of stretching the alert API.

Course illustration
Course illustration

All Rights Reserved.