简体   繁体   English

按下导航后退按钮时如何调用功能?

[英]How can I call a function when the navigation back button is pressed?

I need to call a function when I press the back button. 当我按下后退按钮时,我需要调用一个函数。 Preferably, without having to create another button 优选地,不必创建另一个按钮

Place the following in your view controller. 将以下内容放在视图控制器中。

override func willMove(toParent parent: UIViewController?) {
    super.willMove(toParent: parent)

    if parent == nil {
        // The view is being removed from the stack, so call your function here
    }
}

When parent is nil, it means the view is being removed from the stack (ie the back button was pressed). 当parent为nil时,表示视图正从堆栈中删除(即按下后退按钮)。

One consideration when compared with matt's answer is that willMove is called before viewWillDisappear . 与matt的答案相比,一个考虑因素是在viewWillDisappear之前调用willMove Your mileage will vary based on what your function does, but this can result in issues based on your specific needs. 您的里程将根据您的功能而有所不同,但这可能会导致根据您的具体需求出现问题。 With that said, either answer is perfectly capable of providing what you requested. 话虽如此,答案完全能够满足您的要求。

Implement viewWillDisappear and, in it, test isMovingFromParentViewController . 实现viewWillDisappear并在其中测试isMovingFromParentViewController If the latter is true, you're being popped. 如果后者是真的,那么你就会被弹出。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未按下按钮时如何调用按钮功能 - How do I call a button function when the button is not being pressed 在UITextView中按下返回按钮时,如何调用自定义函数? - How can I call a custom function when the return button is pressed within a UITextView? 迅捷的…如何在按下按钮并将其恢复为正常状态时更改图像? - Swift… how can I make an image change when a button its pressed and return it back to normal? 按下后退按钮时如何执行操作 - How do I perform an operation when the back button is pressed 尝试实现一个在导航栏中按下后退按钮时发出信号的委托-我在做什么错? - Trying to implement a delegate that signals when the back button is pressed in the navigation bar - what am I doing wrong? 当按下导航后退按钮时,将值返回到呈现视图控制器 - Return values to presenting view controller when navigation back button pressed 按下导航后退按钮时,SWRevealViewController关闭左侧视图 - SWRevealViewController close left side view when navigation back button pressed 按下后退按钮时,前一屏幕上的导航标题消失 - navigation title on previous screen disappear when back button pressed Xcode:检测何时按下导航控制器实现的“后退按钮” - Xcode: Detecting when a navigation controller implemented “back button” is pressed 迅速按下导航栏后退按钮时重新加载视图控制器2 - Reloading view controller when navigation bar back button is pressed in swift 2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM