简体   繁体   English

按下“后退”按钮时执行操作

[英]Do things when Back Button is pressed

Inside of navigationController , when I'm in a ChildViewController and when the user taps on Back Button. navigationController内部,当我在ChildViewController以及用户点击“后退”按钮时。 I would love to send some data to the parentViewController before I go away. 我很想在parentViewController之前将一些数据发送到parentViewController

Is any simple why to do this besides using 有什么简单的原因除了使用

-(void)viewWillDisappear;

First, because this ChildViewController also has its own child, and when it gets pushed, viewWillDisappear gets called as well. 首先,由于此ChildViewController也有自己的子级,因此当它被推送时,也会调用viewWillDisappear

Second, when I user hits home button. 其次,当用户点击主页按钮时。 it get called.(I guess) 它被调用了。(我猜)

Please, any reasonable and proper way to do this? 请以任何合理适当的方式做到这一点?

Here's a nice simple solution, that gets around the issue that viewWillDisappear is called when modalViewControllers are shown, or further viewControllers are pushed. 这是一个很好的简单解决方案,解决了在显示modalViewControllers或进一步推送viewControllers时调用viewWillDisappear的问题。 Eg detects the case where we're actually taken off the stack. 例如,检测到实际从堆栈中取出的情况。

-(void)viewWillDisappear:(BOOL)animated {
  NSUInteger ind = [[self.navigationController viewControllers] indexOfObject:self];
  if (ind == NSNotFound) {
      // do something, we're coming off the stack.
  }
}

The advantage of this is that you don't need to subclass UINavigationController. 这样做的好处是您不需要子类化UINavigationController。

Source: http://objectivesea.tumblr.com/post/21705735018/poppingoffthestack 资料来源: http : //objectivesea.tumblr.com/post/21705735018/poppingoffthestack

I would solve this problem using the UINavigationControllerDelegate . 我将使用UINavigationControllerDelegate解决此问题。

In this case, you can use it to keep track of your controllers and you can implement navigationController:willShowViewController:animated: to transfer the data you need between the controllers. 在这种情况下,可以使用它来跟踪控制器,还可以实现navigationController:willShowViewController:animated:在控制器之间传输所需的数据。 It's a lot cleaner than trying to hook into the viewWillDisappear notification, because you can isolate the data transfer logic within that delegate instead of having pieces of it in every controller. 这比尝试挂入viewWillDisappear通知要干净得多,因为您可以隔离该委托中的数据传输逻辑,而不是在每个控制器中都包含它。

You can do this in any of following ways: 您可以通过以下任何一种方式来执行此操作:

  1. Send a notification when the second controller is going away and have the parent listen for it. 当第二个控制器消失时发送通知,让父母听。

  2. Create a delegate protocol that lets the second controller reference a parent method directly. 创建一个委托协议,该协议使第二个控制器直接引用父方法。

  3. Use the parent's viewWillAppear: and check the isMovingToParentViewController property for whether it's just re-appearing. 使用父级的viewWillAppear:并检查isMovingToParentViewController属性是否只是重新出现。

and apply what is best for your value passing conditions and availability of values. 并应用最适合您的价值传递条件和价值可用性的方法。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM