How to get input value from a UIAlertController text field?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
UIAlertController is a convenient way to collect a small piece of user input without building a custom screen. The usual pattern is simple: create the alert, add one or more text fields, and read the text inside the action handler after the user taps a button. The important detail is that the text lives in the alert controller’s textFields array, not in the action itself.
Add a Text Field and Read Its Value
Text fields are only supported when the alert controller uses .alert style. You add the field before presenting the alert, then pull the value out in the submit action.
The key line is alert?.textFields?.first?.text. That is where the entered value is stored when the action handler runs.
Working with More Than One Field
If the alert has multiple inputs, access them by index in the textFields array. Add the fields in a predictable order and read them back in the same order.
This pattern is fine for one or two short values. If the interaction becomes more complex, a dedicated view controller is usually easier to validate, test, and maintain.
Validation and Presentation Details
Most bugs around alert input are not about reading the text. They come from validation and presentation timing.
You should usually trim whitespace, reject empty values, and make sure the alert is presented on the main thread. In UIKit event handlers you are already on the main thread, but if you trigger the alert from an async callback, wrap presentation in DispatchQueue.main.async.
It is also common to prefill the text field with the current value so the alert behaves like an edit dialog rather than a blank prompt. That makes the UI feel more deliberate and reduces accidental data loss.
Common Pitfalls
One mistake is trying to read the text immediately after calling addTextField. At that point, the user has not entered anything yet. Read it inside the action handler that runs after the tap.
Another mistake is using .actionSheet and expecting text fields to appear. Text fields are supported on .alert, not on action sheets.
Developers also sometimes force-unwrap the first text field. That works until the alert configuration changes. Optional access with a sensible fallback is safer.
Finally, avoid treating an alert as a full form. If you need several fields, inline validation, or complex keyboard behavior, build a custom screen instead of stretching UIAlertController beyond its intended use.
Summary
- Add text fields with
addTextFieldbefore presenting the alert. - Read the user’s input from
alert.textFieldsinside the action handler. - Use
.alertstyle, not.actionSheet, when you need text entry. - Trim and validate the value before using it in your UI or network request.
- For larger forms, switch to a dedicated view controller instead of overloading an alert.
Related reading
- How to get input value from a UIAlertController text field?
- How to get iOS device's WiFi IP address?
- How to get Latitude and Longitude of the mobile device in android?
- How to get mathemical PI constant in Swift
- How to get my IP address programmatically on iOS/macOS?
- How to get RGB values from UIColor?
- How to get root view controller?
- How to get screen dimensions as pixels in Android
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.