How do I make nothing happen with this cancel action let cancelAction =…
- User
How do I make nothing happen with this cancel action
let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: { [weak self] _ in
}) - Assistant
Your code snippet already defines a
cancelActionfor an alert controller in Swift that does effectively "nothing" when selected. When the "Cancel" action is tapped, the closure provided as the handler executes. In your case, the closure is empty, so no action will be taken.Here's a more explicit example for creating and presenting an alert with a cancel action that does nothing in a
UIViewController. This might help if you want to see the complete picture:import UIKit class YourViewController: UIViewController { func showAlert() { // Create an alert controller let alert = UIAlertController(title: "Alert Title", message: "Your message here.", preferredStyle: .alert) // Create a UIAlertAction which represents the cancel action let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (action) in // This is where nothing happens. You can add a print statement for debug purposes print("Cancel action tapped, no further action taken.") // No other code is needed, as you want to make nothing happen } // Now, add this action to your alert controller alert.addAction(cancelAction) // Present the alert to the user present(alert, animated: true) } }