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:
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.
For passwords:
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.
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.
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
.actionSheetand expecting text fields to appear. - Reading
alert.textFieldsoutside 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
UIAlertControlleronly when using.alertstyle. - Configure each field inside
addTextField. - Read the values from
alert.textFieldsin 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.

