开发者

Better Way to Pass Data from child to parent in UINavigationController, and modal presentations: reference to parent or delegates?

So lately I have been trying ways to pass data from a child to a parent view controller. I have settled with two. The delegates approach:

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

and the simple approach of just passing reference of the parent view controller to the child view controller before the child controller is pushed (UINavigationController) or presented (via modal presentation)

- (IBAction)myAction:(id)sender{
    MyViewController *myView = 
        [[MyViewController alloc] init];

    myView.isLinking = YES;
    myView.parent = self;


    // present child pushing or presentation logic here

    [sender resignFirstResponder];
}

but I'm wonderin开发者_开发问答g why should I go with delegates if I can do the latter when I only just want to be able to pass data from child to parent view controllers?

Also, there's only about a few answers on SO where doing the latter is recommended. In fact, I can't remember if there are.

So I was wondering, why is doing the latter not recommended and why are there more people suggesting delegates or even retrieving the controller from the application delegate?


Because the second approach ties both the parent to the child and vice versa, making a circular dependency. This is bad OOP. Using delegates means the child view can be used from any manner of calling code, now or in the future.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜