简体   繁体   English

按下后退按钮时如何执行操作

[英]How do I perform an operation when the back button is pressed

I have two navigation Controllers for my views. 我的视图有两个导航控制器。 I need to perform some operations when the user presses the back button to go to the previous view. 当用户按下“后退”按钮转到上一个视图时,我需要执行一些操作。 Normally I would do this using the prepareForSague() function and using the sague Identifier. 通常,我将使用prepareForSague()函数并使用sague标识符来执行此操作。

What's the identifier (or how do I get it) of the back button that comes with navigation controller so i can use it in my prepareForSague function. 导航控制器随附的后退按钮的标识符是什么(或如何获取),因此我可以在我的prepareForSague函数中使用它。 Or is it done in other ways? 还是以其他方式完成?

Please follow the steps: 请按照以下步骤操作:

  1. I think you have to first create custom button 我认为您必须先创建自定义按钮
  2. Assign it as back button of Navigation controller. 将其分配为导航控制器的后退按钮。
  3. Add target selector to that button and use popToViewController : method. 将目标选择器添加到该按钮,然后使用popToViewController :方法。

Or 要么

  1. Do connection with back button 使用后退按钮进行连接
  2. Add action to it 给它添加动作
  3. write your back button logic in that method. 用该方法编写后退按钮逻辑。

Just place your code inside viewWillDissapear 只需将您的代码放在viewWillDissapear中

    override func viewWillDisappear(animated: Bool)

That is, don't try to change the behavior of the back button, just write whatever code you need to be executed inside viewWillDissapear 也就是说,不要尝试更改后退按钮的行为,只需在viewWillDissapear内编写需要执行的任何代码即可

you have to use "viewWillDissapear" method for this, if you wanna use default back button. 如果要使用默认的后退按钮,则必须使用“ viewWillDissapear”方法。

But you may have to be careful here coz you only need this to work when really going back by back button, so every other actin - which calls "viewWillDissapear" method, you may need to make a flag to know its not the back action. 但是您可能需要注意,因为只有在真正通过后退按钮返回时才需要使用此功能,因此每隔一个肌动蛋白-调用“ viewWillDissapear”方法,您可能需要标记以知道其不是后退动作。

One way of doing this would be to use the ViewWillDisappear() function to detect when the navigation back button is pressed. 一种方法是使用ViewWillDisappear()函数检测何时按下导航后退按钮。 Found the answer here. 这里找到答案

您的类可以实现UINavigationControllerDelegate协议并实现此方法:

- (void)navigationController:(UINavigationController*)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated

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

相关问题 (iOS)按下键盘的后退按钮时如何获得通知? - (iOS) How do I get notified when the keyboard's back button is pressed? 按下“后退”按钮时执行操作 - Do things when Back Button is pressed 按下导航后退按钮时如何调用功能? - How can I call a function when the navigation back button is pressed? 未按下按钮时如何调用按钮功能 - How do I call a button function when the button is not being pressed 按下后退按钮时如何实际捕捉? - How to actually catch when back button is pressed? 如何在按下按钮时更改按钮的颜色,并在按下其他按钮时重置为原始颜色? - How do I change a button's color when pressed and reset to original color when a different button is pressed? 如何在按下按钮时使按钮弹出警报视图并返回主视图控制器? - How Do I Make A Button Get An Alert View To Pop Up When Pressed And Go Back To The Main View Controller? 当按下TableView单元格中的按钮时,我怎么知道按下了哪一行单元格的按钮? - When the button in TableView cell pressed, how do I know the button of which row of cell pressed? 按下按钮时如何返回下一个数组? - How do I return the next array when a button is pressed? 按下按钮时如何更改图像? - How do I change an Image when a button is pressed?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM