代表VS.iPhoneOS 中的通知 [英] Delegates Vs. Notifications in iPhoneOS

查看:9
本文介绍了代表VS.iPhoneOS 中的通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从子视图控制器调用根视图控制器中的方法,这样当我更改选项时,它们将自动更新根视图,进而更新其他几个视图控制器.对于第二部分,我使用了通知,但对于第一部分,我尝试使用委托,因为它(因此我一直相信)是一种很好的编程实践.我在使它工作时遇到问题,并且知道我可以轻松设置另一个通知来完成这项工作.我应该继续尝试实现委托还是只使用通知?

I am trying to call a method in my root view controller from a child view controller such that when I change my options they will automatically update the root view, which will in turn update several other view controllers. For the second part I have used notifications, but for this first I am trying to use a delegate because it (so I have been lead to believe) is a good programming practice. I am having trouble making it work and know that I can set up another notification easily to do the job. Should I continue trying to implement the delegate or just use a notification?

推荐答案

委派在许多情况下都是一种很好的编程实践,但这并不意味着如果您对它不满意就必须使用它.委托和通知都有助于将视图控制器彼此分离,这是一件好事.通知可能更容易编码并提供多个对象可以观察一个通知的优势.对于委托,这种事情不能在不修改委托对象的情况下完成(而且很不寻常).

Delegating is a good programming practice for many situations but that doesn't mean you have to use it if you're not comfortable with it. Both delegating and notifications help decouple the view controllers from each other, which is a good thing. Notifications might be a little easier to code and offer the advantage that multiple objects can observe one notification. With delegates, such a thing cannot be done without modifying the delegating object (and is unusual).

委托的一些优点:

  • 委托对象和委托之间的联系更加清晰,尤其是在强制实现委托的情况下.
  • 如果必须将不止一种类型的消息从受托人传递到受托人,委托可以通过为每个消息指定一个委托方法来使这一点更加清晰.对于通知,您可以使用多个通知名称,但所有通知在观察者一侧以相同的方法结束(可能需要一个讨厌的 switch 语句).

只有您可以决定哪种模式更适合您.在任何情况下,您都应该考虑不要让您的视图控制器发送通知或委托消息.在许多情况下,视图控制器应该改变模型,然后模型应该通知它的观察者或它的委托它已经改变了.

Only you can decide what pattern is more appropriate for you. In any case, you should consider not having your view controller send the notification or the delegate message. In many cases, the view controller should change the model and then the model should inform its observers or its delegate that it has been changed.

实现委托模式很简单:

  1. 在您的 ChildViewController.h 中,声明委托必须稍后实现的委托协议:

  1. In your ChildViewController.h, declare the delegate protocol that the delegate must implement later:

@protocol ChildViewControllerDelegate <NSObject>
@optional
- (void)viewControllerDidChange:(ChildViewController *)controller;
@end

  • 在文件的顶部,创建一个实例变量来保存指向 ChildViewController 中的委托的指针:

  • At the top of the file, create an instance variable to hold the pointer to the delegate in your ChildViewController:

    @protocol ChildViewControllerDelegate;
    @interface ChildViewController : UIViewController {
        id <ChildViewControllerDelegate> delegate;
        ...
    }
    @property (assign) id <ChildViewControllerDelegate> delegate;
    ...
    @end
    

  • 在 RootViewController.h 中,使您的类符合委托协议:

  • In RootViewController.h, make your class conform to the delegate protocol:

    @interface RootViewController : UIViewController <ChildViewControllerDelegate> {
    ...
    

  • 在 RootViewController 实现中,实现委托方法.此外,当您创建 ChildViewController 实例时,您必须分配委托.

  • In the RootViewController implementation, implement the delegate method. Also, when you create the ChildViewController instance, you have to assign the delegate.

    @implement RootViewController
    ...
    // in some method:
    ChildViewController *controller = [[ChildViewController alloc] initWithNibName:...
    controller.delegate = self;
    ...
    - (void)viewControllerDidChange:(ChildViewController *)controller {
        NSLog(@"Delegate method was called.");
    }
    ...
    

  • 在 ChildViewController 实现中,在适当的时候调用委托方法:

  • In the ChildViewController implementation, call the delegate method at the appropriate time:

    @implementation ChildViewController
    ...
    // in some method:
    if ([self.delegate respondsToSelector:@selector(viewControllerDidChange:)]) {
        [self.delegate viewControllerDidChange:self];
    }
    ...
    

  • 就是这样.(注意:我是凭记忆写的,所以里面可能有一些错别字/错误.)

    That's it. (Note: I have written this from memory so there are probably some typos/bugs in it.)

    这篇关于代表VS.iPhoneOS 中的通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆