Home > Back-end >  How do I dismiss the alert in Swift after it's presented?
How do I dismiss the alert in Swift after it's presented?

Time:11-11

I have a simple alert that I would like the user to be able to dismiss. Just looking for the easiest way to do this in Swift 5 / iOS 15

present(alert, animated: true, completion: nil)

Edit: Figured out the answer. Hope this will help out someone in the future who has the same question as me. (Mods, if you don't think it will help anyone, feel free to delete this thread)

        
// dismiss alert after 2 seconds
        DispatchQueue.main.asyncAfter(deadline: .now()   2.0, execute: {
            alert.dismiss(animated: true, completion: nil)
        })

CodePudding user response:

add this code when you want to hide

self.dismiss(animated: true)

CodePudding user response:

thanks for all the responses! I figured it out after looking up another thread. No, I don't have any buttons on the alert or any interaction for the user, that's why it was hard for me to find an answer originally. Thanks everyone for being patient with my noobness!

        present(alert, animated: true, completion: nil)
        
// dismiss alert after 2 seconds
        DispatchQueue.main.asyncAfter(deadline: .now()   2.0, execute: {
            alert.dismiss(animated: true, completion: nil)
        })

  • Related