show alert when user tries to go back navigation controller swift 5 code example
Example 1: xcode execute code after when navigating back to screen
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
if self.isMovingFromParent {
}
}
Example 2: xcode execute code after when navigating back to screen
override func viewDidLoad {
super.viewDidLoad()
self.navigationItem.hidesBackButton = true
let newBackButton = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.plain, target: self, action: #selector(YourViewController.back(sender:)))
self.navigationItem.leftBarButtonItem = newBackButton
}
@objc func back(sender: UIBarButtonItem) {
_ = navigationController?.popViewController(animated: true)
}