UIAlertView in Swift: 5 Fatal Mistakes and How to Avoid Them

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: Why Your UIAlertView Doesn't Work (and What Stack Overflow Has to Do With It)

\n\n

If you've ever Googled "How would I create a UIAlertView in Swift?" — you're not alone. This question on Stack Overflow has gathered hundreds of thousands of views. What could be simpler, right? Show the user a popup window with an "OK" button. But in practice, beginners (and not only) keep stepping on the same rake.

\n\n

In this article, we'll break down the top 5 mistakes when creating UIAlertView in Swift, show how to fix them, and provide ready-to-use code for UIAlertController — the modern replacement for the deprecated API. If you want to write clean, working code without crashes — stick with us.

\n\n

Mistake #1: Using UIAlertView Instead of UIAlertController

\n\n

The most common mistake is trying to call UIAlertView in modern Swift. This class was deprecated back in iOS 8. If you write:

\n\n
let alert = UIAlertView(title: "Error", message: "Something went wrong", delegate: nil, cancelButtonTitle: "OK")\nalert.show()
\n\n

…the compiler will likely give a warning or an error. And if the project builds — at best, the alert won't work correctly on iOS 13+.

\n\n

Why does this happen?

\n

Apple replaced UIAlertView with UIAlertController back in iOS 8. The old class is no longer supported, and using it can lead to unexpected behavior.

\n\n

How to fix it?

\n

Use UIAlertController with the preferred style .alert or .actionSheet. Here's the correct code:

\n\n
let alert = UIAlertController(title: "Error",\n                              message: "Something went wrong",\n                              preferredStyle: .alert)\nalert.addAction(UIAlertAction(title: "OK", style: .default))\npresent(alert, animated: true)
\n\n

Remember: in modern Swift, UIAlertView is legacy. Always use UIAlertController.

\n\n

Mistake #2: Forgetting to Call present()

\n\n

Even if you create a UIAlertController but forget to show it — nothing will happen. Literally. The code will execute, the alert will be created in memory, but the user won't see it.

\n\n

Typical error example:

\n\n
func showAlert() {\n    let alert = UIAlertController(title: "Hello", message: "World", preferredStyle: .alert)\n    alert.addAction(UIAlertAction(title: "OK", style: .default))\n    // Forgot present!\n}
\n\n

Result: silence. No error, no alert. You'll be wondering why the code doesn't work.

\n\n

How to fix it?

\n

Always call present(_:animated:completion:) on the current view controller:

\n\n
func showAlert() {\n    let alert = UIAlertController(title: "Hello", message: "World", preferredStyle: .alert)\n    alert.addAction(UIAlertAction(title: "OK", style: .default))\n    present(alert, animated: true, completion: nil)\n}
\n\n

Tip: if you call the alert from a closure or asynchronous code, make sure self (the view controller) still exists. Otherwise, you'll get a crash.

\n\n

Mistake #3: Incorrect Button Press Handling

\n\n

In the old UIAlertView, we used the delegate alertView(_:clickedButtonAt:). In UIAlertController, it's different — closures are used.

\n\n

Beginners often try to make the class conform to UIAlertViewDelegate and implement delegate methods, but this won't work with UIAlertController.

\n\n

Incorrect:

\n\n
class ViewController: UIViewController, UIAlertViewDelegate {\n    func showAlert() {\n        let alert = UIAlertController(title: "Question", message: "Do you agree?", preferredStyle: .alert)\n        alert.addAction(UIAlertAction(titl                

Blogs

Book Recommendations