Dismissing AlertController When Pushed To Next ViewController

Hi, I have created a custom AlertController and I present it usin the rootViewController like below:

window.rootViewController?.present(alert, animated: true, completion: nil)

This works fine, but, if I navigate to any other screen without dismissing the alert, like in below code:

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
    
navigationController?.pushViewController(viewController, animated: true)

the AlertController is still in place, how to dismiss the alert when pushed to another ViewController?

You could put the second snippet code in the completion handler of alert buttons.

But, how is the alert defined ?

Normally, you must close the alert for navigation to another VC to fire.

.

This works fine, but, if I navigate to any other screen without dismissing the alert, like in below code:

How do you do it ? So please show more code to see the snippets in their context.

I'm not sure I understand the problem here. alert is (presumably) a UIViewController subclass, which means you can call alert.dismiss to dismiss it programmatically. Can't you just call that before using SecondViewController?

The dismiss function has a completion handler closure, so if you want to "wait" for it to be fully dismissed before you transition to the new view controller, you can put the code in your second snippet into the completion handler.

So, basically what I'm doing is, simply pushing to second view controller like below:

@IBAction func showAlertButtonTapped(_ sender: UIButton) {

     _ = AlertUtil.sharedInstance.showAlertView(message: "This is an alert message.", actionTitles: ["OK"], actions: nil)             let storyboard = UIStoryboard(name: "Main", bundle: nil)      let viewController = storyboard.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController      
navigationController?.pushViewController(viewController, animated: true)    }

What I want is to write a common code in my showAlertView function, so that, whenever I navigate to another view controller the alert should be dismissed.

It is good practice to format code with code formatter:

@IBAction func showAlertButtonTapped(_ sender: UIButton) {
    _ = AlertUtil.sharedInstance.showAlertView(message: "This is an alert message.", actionTitles: ["OK"], actions: nil)
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let viewController = storyboard.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
    
    navigationController?.pushViewController(viewController, animated: true)
}

I understand that AlertUtil is a class of yours.

If I read correctly, it has an OK button which has no action ?

So you want the alert would disappear immediately, without user having time to notice ?

.

the AlertController is still in place, how to dismiss the alert when pushed to another ViewController?

You have several options:

  • have an action for OK button where you pushViewController

or

  • define a var in first VC to keep a reference of the alert
    myAlert = AlertUtil.sharedInstance.showAlertView(message: "This is an alert message.", actionTitles: ["OK"], actions: nil)
  • in the pushed controller, dismiss myAlert of the previousController
navigationController?.previousViewController.myAlert.dismiss()
  • you can define an extension to get the previous controller:
extension UINavigationController {
    var previousViewController: UIViewController? {
       viewControllers.count > 1 ? viewControllers[viewControllers.count - 2] : nil
    }
}

Okay, got it. Thanks!

Just wanted to know whether it is a good practice to present alert using rootViewController or not?

Dismissing AlertController When Pushed To Next ViewController
 
 
Q