Avatar ·

How to create UIAlertView in Swift? (iOS, UIKit)

I just started learning Swift and iOS development. I'm writing my first app — a simple to-do list. I want to show an alert when trying to delete a task, so the user can confirm the action.

\n\n

In old tutorials I see UIAlertView with delegates and an initializer like:

\n\n
\nlet alert = UIAlertView(title: "Attention", message: "Are you sure?", delegate: self, cancelButtonTitle: "No", otherButtonTitles: "Yes")\nalert.show()\n
\n\n

But Xcode underlines UIAlertView in red and says it's deprecated in iOS 9. I'm using Xcode 14 and iOS 16. How do I properly show alerts in modern Swift? I need an example with buttons and handling taps.

\n\n

My current code (which doesn't work):

\n\n
<