views:

23

answers:

2

Hi,

I am having trouble passing data from a childView to the parentView inside a navigationController when the user click on the back button.

I tried using the viewWillDisappear and it is not being called.

Any ideias?

+2  A: 

There are two basic techniques for doing this:

Using a Delegate

Define a simple delegate protocol for your child. Like for example if your child is used to pick some string from a list of things then do something like:

@protocol ChildViewControllerDelegate <NSObject>
-(void) childView: (ChildView*) didPickNameFromList: (NSString*) name;
@end

Your parent would simply implement this delegate protocol and will have a change to be updated when a selection is made in the client.

This is a very standard technique that is also used by many of the view controllers that come with iOS. For example take a look at the ABPeoplePickerNavigationControllerDelegate.

Using a Shared (Container) object)

The other technique that you can use is to use a shared object in which the child view controller can change a value. The parent view controller created this object and keeps a reference to it. When it displays the child view controller then it passes a reference to the shared object to it so that the child can set values on it. Then when the parent appears again, it can update its status based on the values of that shared object.

Personally I prefer technique #1.

St3fan
A: 

Actually I did something different.

I declared a variable with the type of the parentController in the child controller.

And in the parentController before I push the childController I do this: objView.parentController = self;

Then I just call the parentController.variableIwant = something; On the child controller and Voile!

Joao Henrique
Technically that will work, but for maximum encapsulation and reusability, creating a delegate is the best way to go
Brian