mercredi 22 juin 2016

Passing data to parent view controller when pressing back navigation arrow

I've seen many ways of passing data from a view controller back to its parent in the navigation stack and even though I think the case that I'm going to describe should be quite common, none of these seem to solve it. What I want to do:

  1. Push a child view controller onto the navigation stack. I set up its properties in the parent view controller's prepareForSegue(_:sender).
  2. Edit the properties of the child view controller. Until here everything works.
  3. When tapping the arrow back button (in the top left corner), the child view controller should he popped (which happens automatically) and needs to pass the edited data back to the parent.

This would be easy if I could set up an unwind action in the storyboard, but this is not possible since the arrow back button is displayed only at runtime. prepareForSegue(_:sender) doesn't seem to be called on the child when pressing that arrow. Neither are canPerformUnwindSegueAction(_:fromViewController:withSender) or unwindForSegue(_:towardsViewController) or segueForUnwindingToViewController(_:fromViewController:identifier:) on either the child or the parent.

As suggested here I could override the child's viewWillDisappear method and access the parent from the navigation stack, but there must be a way for the parent to access the child somewhere, or this would be bad design, right? I'm grateful for any help! I'm targeting iOS 7+.

Aucun commentaire:

Enregistrer un commentaire