Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass back data to parent view when using UINavigationController and default Back Button?

Background - So in terms of approach at the high level there to me seems to be:

  1. Save/persist settings as you proceed through a chain of UINavigationController levels - so when initially starting to change settings, or create settings, there would need to a default set of values - i.e. so at any point of time if the application dies the settings would be valid
  2. save up changes as you go, and once you come back out of the UINavigationController tree of screens (with all settings set) there would be a point where you can save

This question is focusing on Option 2 which I was trying to implement.

The Plan - Whilst in a settings type UINavigationController I was going to pass existing (or default) details for one section of the settings from the parent view to the child view when the child view gets pushed onto the stack. The issue is when this data is updated, AND assuming I want to stick with the default Back Button, there does not seem to be a way to intercept the default Back Button, so that when I'm ready to pop the child off the stack just before this I will call a delegate which will pass the latest settings back to the parent.

QUESTION - How to pass updated data from a child controller back to the parent controller when using a UINavigationController and wanting to stick with the default Back Button (with the left arrow thing on it).

That is, probably I have isn't how to pass the data back I guess (I'm going to use a delegate), but rather how to hook into a callback method at the right point of time in the child controller so as to then use the delegate method to pass the data back.

like image 983
Greg Avatar asked Apr 05 '11 02:04

Greg


1 Answers

Using Delegate might be the solution for you.

Tutorial can be found:

http://timneill.net/2010/11/modal-view-controller-example-part-2/

which is very helpful for me.

Edit:

  1. Sounds like you need to make a back button yourself, info:

UINavigationController "back button" custom text?

and then add method:

[self.navigationController popViewControllerAnimated:YES]; 

to go back to the previous view. You can then add your own code to that back button.

  1. Even simpler solution: implement 'viewWillDisapper' method.

This gets called every time back button is pressed. But watch out if it goes to another view instead of going back.

like image 83
Xiao Avatar answered Oct 03 '22 09:10

Xiao