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
}
}