Adding a simple UIAlertView
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Despite the title, modern iOS apps should not create UIAlertView. Apple deprecated that class long ago, and UIAlertController is the supported API for both alerts and action sheets. If you want a simple message with one or two buttons, the right answer today is to present a UIAlertController from the current view controller.
Presenting a Basic Alert
The simplest alert has a title, a message, and one or more actions. Each action can be .default, .cancel, or .destructive, and each one can run a handler when the user taps it.
This code is enough for most confirmation dialogs. The view controller builds the alert, adds actions, and presents it. The destructive action uses a weak capture of self so the alert handler does not accidentally keep the view controller alive longer than necessary.
Adding Text Input
Alerts can also collect a small amount of input. A common example is asking for a folder name, an email address, or a one-time code. UIAlertController lets you add text fields only when the preferred style is .alert.
This pattern keeps the UI lightweight. The alert owns the text field, and the save action reads the value when the user confirms. For anything more complex than one or two fields, a dedicated view controller is usually easier to validate and test.
When the Old UIAlertView Name Still Matters
You will still see UIAlertView in old blog posts, legacy Objective-C code, and migration questions. The concepts map cleanly to modern UIKit:
- '
UIAlertViewtitle becomes theUIAlertControllertitle.' - '
UIAlertViewmessage becomes theUIAlertControllermessage.' - Button callbacks become
UIAlertActionhandlers.
If you are maintaining an older app, replacing UIAlertView with UIAlertController is usually straightforward. The main difference is that alerts are now presented by a view controller rather than shown directly by the alert object itself.
Common Pitfalls
The most common mistake is trying to use UIAlertView in new code. It is deprecated, and relying on deprecated UIKit APIs makes future maintenance harder. Use UIAlertController unless you are reading very old sample code.
Another frequent issue is presenting the alert from the wrong place. If the current view controller is not in the visible hierarchy, present(_:animated:) may log a warning and nothing appears. Present from the controller that is currently on screen.
Action sheets need extra care on iPad. If you use .actionSheet, you must provide an anchor with popoverPresentationController?.sourceView, sourceRect, or barButtonItem. Without that anchor, the app can crash at runtime on iPad.
Finally, keep action handlers short. Launching long-running work directly in the tap handler can make the interface feel frozen. Trigger the next operation quickly, then move any expensive work to the appropriate async path.
Summary
- Use
UIAlertControllerfor simple alerts in modern iOS code. - Add actions with
UIAlertActionand present the alert from a visible view controller. - Use text fields only with the
.alertstyle and keep the interaction small. - Replace legacy
UIAlertViewreferences withUIAlertControllerwhen updating older code. - Remember the iPad anchor requirement for action sheets.

