I would like to perform some actions when a user presses the "Back" button on my Navigation Controller. Is there a Swift function that is called when this happens?
当用户按下导航控制器上的“后退”按钮时,我想执行一些操作。当发生这种情况时,是否有一个Swift函数被调用?
3 个解决方案
#1
3
override func viewWillDisappear(animated: Bool) {
// Do Your Lines of Code ...
}
Everytime when back button or Done is pressed or a view is popped out this function is called.. you need to override this..
每次按下后退按钮或完成按钮或弹出视图时,都会调用此功能..您需要覆盖此...
#2
4
Try this (copied and pasted from manecosta)
试试这个(从manecosta复制并粘贴)
Replacing the button to a custom one as suggested on another answer is possibly not a great idea as you will lose the default behavior and style.
按照另一个答案的建议将按钮替换为自定义按钮可能不是一个好主意,因为您将失去默认行为和样式。
One other option you have is to implement the viewWillDisappear method on the View Controller and check for a property named isMovingFromParentViewController. If that property is true, it means the View Controller is disappearing because it's being removed (popped).
另一个选项是在View Controller上实现viewWillDisappear方法,并检查名为isMovingFromParentViewController的属性。如果该属性为true,则表示视图控制器正在消失,因为它正被删除(弹出)。
Should look something like:
应该看起来像:
override func viewWillDisappear(animated : Bool) {
super.viewWillDisappear(animated)
if (self.isMovingFromParentViewController()){
// Your code...
}
}
Here is the link to the other question
这是另一个问题的链接
#3
2
Try this:
尝试这个:
override func willMoveToParentViewController(parent: UIViewController?) {
if parent == nil {
// Back button Event handler
}
}
#1
3
override func viewWillDisappear(animated: Bool) {
// Do Your Lines of Code ...
}
Everytime when back button or Done is pressed or a view is popped out this function is called.. you need to override this..
每次按下后退按钮或完成按钮或弹出视图时,都会调用此功能..您需要覆盖此...
#2
4
Try this (copied and pasted from manecosta)
试试这个(从manecosta复制并粘贴)
Replacing the button to a custom one as suggested on another answer is possibly not a great idea as you will lose the default behavior and style.
按照另一个答案的建议将按钮替换为自定义按钮可能不是一个好主意,因为您将失去默认行为和样式。
One other option you have is to implement the viewWillDisappear method on the View Controller and check for a property named isMovingFromParentViewController. If that property is true, it means the View Controller is disappearing because it's being removed (popped).
另一个选项是在View Controller上实现viewWillDisappear方法,并检查名为isMovingFromParentViewController的属性。如果该属性为true,则表示视图控制器正在消失,因为它正被删除(弹出)。
Should look something like:
应该看起来像:
override func viewWillDisappear(animated : Bool) {
super.viewWillDisappear(animated)
if (self.isMovingFromParentViewController()){
// Your code...
}
}
Here is the link to the other question
这是另一个问题的链接
#3
2
Try this:
尝试这个:
override func willMoveToParentViewController(parent: UIViewController?) {
if parent == nil {
// Back button Event handler
}
}