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 supports adding text fields to alert dialogs for collecting user input. To get the text value, access the textFields array inside the action handler closure after the user taps a button. The text field is added with addTextField, configured in a closure, and read back via alertController.textFields?[0].text when the user confirms.
Basic Text Input Alert
The key pattern: alert.textFields?[0].text inside the action handler accesses the first text field's value after the user taps OK.
Multiple Text Fields
Text fields are indexed in the order they were added. The first addTextField call creates index 0, the second creates index 1.
Configuring Text Fields
Disabling the OK Button Until Input Is Valid
Using a Completion Handler
For reusability, wrap the alert in a function with a completion handler:
async/await Version (iOS 13+)
Common Pitfalls
- Accessing
textFieldsoutside the action handler: The text fields exist when the alert is presented, but their values are only meaningful after the user taps an action button. Readingalert.textFields?[0].textimmediately afterpresent()returns the initial (empty or default) value, not what the user typed. - Using
.actionSheetstyle with text fields:UIAlertControllerwith.actionSheetstyle does not support text fields. CallingaddTextFieldon an action sheet crashes at runtime. Always use.alertstyle for text input. - Forgetting optional chaining:
alert.textFieldsis optional, andtexton aUITextFieldis also optional. Usingalert.textFields![0].text!crashes if the array is empty or text is nil. Always usealert.textFields?[0].text ?? "". - Strong reference to
selfin action closures: Action handler closures captureselfstrongly by default. For view controllers, this can delay deallocation. Use[weak self]in the closure if the alert handler triggers async work. - Not presenting on the main thread:
UIAlertControllermust be presented from the main thread. If you callpresent()from a background queue (e.g., inside a network callback), the alert may not appear or may crash.
Summary
- Add text fields with
alert.addTextField { ... }and configure in the closure - Read values via
alert.textFields?[0].textinside the action handler - Multiple text fields are indexed in the order added (0, 1, 2, ...)
- Use
NotificationCenterto observe text changes and disable the OK button until input is valid - Only
.alertstyle supports text fields —.actionSheetdoes not - Wrap in a completion handler or
async/awaitfor reusable input prompts
Related reading
- 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
- How To Get Selected Value From UIPickerView
.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 courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my 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.