Вы не можете перехватить и удержать пользователя в том же ViewController
после срабатывания viewWillDissaper
.Вы должны сделать это заранее.Я предлагаю добавить UIBarButton
с меткой Back to navigationItem.leftBarButtonItem
и вручную проверить, хочет ли пользователь перемещаться или нет.Это самое близкое, что вы можете получить.
также вы можете использовать UIAlertViewController
для подтверждения того, хочет ли пользователь перемещаться или нет.
//inside viewDidLoad
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Back", style: .plain, target: self, action: #selector(handleBack))
//define another function to handle the selctor
@objc func handleCancel(){
if someConditions {
promptUIAlert()
}
}
//you function should be like this with UIAlertController
func promptUIAlert(){
let alertController = UIAlertController(title: "Error", message: "Some message", preferredStyle: .alert)
let CancelAction = UIAlertAction(title: "Cancel", style: .default) { (action) in
//type your custom code here for cancel action
}
let OkAction = UIAlertAction(title: "OK", style: .default) { (action) in
//type your custom code here for OK action
}
alertController.addAction(OKAction)
alertController.addAction(CancelAction)
self.present(alertController, animated: true)
}
Выполнение чего-либо внутри viewWillDissaper
было бы удобно для сохранения некоторых несохраненных данных позадиэкран.Но не запрашивать и не спрашивать пользователя, хотят ли они остаться в том же ViewController.