开发者

Passing data from childView do parentView!

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 bei开发者_JS百科ng called.

Any ideias?


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.


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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜