答案 0 :(得分:4)
override func viewWillDisappear(animated: Bool) {
// Do Your Lines of Code ...
}
每次按下后退按钮或完成按钮或弹出视图时,都会调用此功能..你需要覆盖它..
答案 1 :(得分:4)
试试这个(从manecosta复制并粘贴)
按照另一个答案的建议将按钮更换为自定义按钮可能不是一个好主意,因为您将失去默认行为和样式。
另一个选项是在View Controller上实现viewWillDisappear方法,并检查名为isMovingFromParentViewController的属性。如果该属性为true,则表示View Controller正在消失,因为它已被删除(弹出)。
应该看起来像:
override func viewWillDisappear(animated : Bool) {
super.viewWillDisappear(animated)
if (self.isMovingFromParentViewController()){
// Your code...
}
}
Here是其他问题的链接
答案 2 :(得分:2)
试试这个:
override func willMoveToParentViewController(parent: UIViewController?) {
if parent == nil {
// Back button Event handler
}
}
答案 3 :(得分:0)
尝试致电
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
if self.isMovingFromParent {
print("isMovingFromParent()") // dismiss for push viewController
}
if self.isBeingDismissed {
print("isBeingDismissed()") // dismiss for modal that doesn't has navigationController
}
if self.navigationController?.isBeingDismissed ?? false {
print("navigationController?.isBeingDismissed()") // dismiss for modal that has navigationController
}
}