Introduction: Why Your UIAlertView Doesn't Work (and What Stack Overflow Has to Do With It)
\n\nIf 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\nIn 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\nMistake #1: Using UIAlertView Instead of UIAlertController
\n\nThe most common mistake is trying to call UIAlertView in modern Swift. This class was deprecated back in iOS 8. If you write:
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\nWhy does this happen?
\nApple replaced UIAlertView with UIAlertController back in iOS 8. The old class is no longer supported, and using it can lead to unexpected behavior.
How to fix it?
\nUse UIAlertController with the preferred style .alert or .actionSheet. Here's the correct code:
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\nRemember: in modern Swift, UIAlertView is legacy. Always use UIAlertController.
Mistake #2: Forgetting to Call present()
\n\nEven 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.
Typical error example:
\n\nfunc showAlert() {\n let alert = UIAlertController(title: "Hello", message: "World", preferredStyle: .alert)\n alert.addAction(UIAlertAction(title: "OK", style: .default))\n // Forgot present!\n}\n\nResult: silence. No error, no alert. You'll be wondering why the code doesn't work.
\n\nHow to fix it?
\nAlways call present(_:animated:completion:) on the current view controller:
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\nTip: 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.
Mistake #3: Incorrect Button Press Handling
\n\nIn the old UIAlertView, we used the delegate alertView(_:clickedButtonAt:). In UIAlertController, it's different — closures are used.
Beginners often try to make the class conform to UIAlertViewDelegate and implement delegate methods, but this won't work with UIAlertController.
Incorrect:
\n\nclass ViewController: UIViewController, UIAlertViewDelegate {\n func showAlert() {\n let alert = UIAlertController(title: "Question", message: "Do you agree?", preferredStyle: .alert)\n alert.addAction(UIAlertAction(titl