How should I really remove UIAlertview from view - Text run over the previous text bug

37 Views Asked by At

Hello everyone I got weired bug which I can't solve - I dig over the internet and couldn't find any solution. I got a viewcontroller screen with 6 buttons on it. I want to show the user a UIAlertview with a description text any time when clicks on one of those buttons. The problem is that in the first button I got the text right but the problem begins from the second button and so on. The new UIAlertview shows the new description text together with the old text so the new text can't be read.

From the debuger I found that the old UIAlertview are there but just not shown on the main screen.

What is the right way to remove it from the view controller?

This is my dismiss function:

 @objc func dismissAlert() {
    guard let targetView = mytargetView else {
        return
    }
    UIView.animate(withDuration: 0.25, animations: {self.alertView.frame = CGRect(x: 40, y: targetView.frame.size.height, width: targetView.frame.size.width-80, height: 300)}, completion: {done in
        if done {
            UIView.animate(withDuration: 0.25, animations: {
                self.backgroundView.alpha = 0
            }, completion: { done in
                if done {
                    self.alertView.removeFromSuperview()
                    self.backgroundView.removeFromSuperview()
                }
            })
        }
    })
}

The previous UIAlerts are still in the background

1

There are 1 best solutions below

2
Malik On

The dismissAlert method is not the proper way to dismiss an alert view. You can simply call the dismiss method on any alert view to properly dismiss it.

let alert = UIAlertController(title: "Some Title", message: "Some message", preferredStyle: .alert)

// Presenting an alert
present(alert, animated: true, completion: nil)

// Dismissing an alert
alert.dismiss(animated: true, completion: nil)