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

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

问题描述

我试图从一个子视图控制器的根视图控制器中调用一个方法,这样当我更改我的选项时,它会自动更新根视图,这将依次更新其他几个视图控制器。对于第二部分,我已经使用通知,但是首先我试图使用一个委托,因为它(因此我一直以为相信)是一个很好的编程实践。我有麻烦让它工作,知道我可以轻松地设置另一个通知来做这项工作。我应该继续尝试实现代表还是只是使用通知?

解决方案

在许多情况下,委托是一个很好的编程实践,但这并不意味着你必须使用它很舒服委托和通知都有助于将视图控制器彼此分离,这是一件好事。通知可能会更容易编码,并提供多个对象可以观察到一个通知的优势。与代表一样,这样的事情不能在不修改委托对象的情况下(并且是不寻常的)。



委托的一些优点:




  • 委托对象和委托之间的连接变得更加清晰,特别是如果实现代理是强制性的。

  • 如果有多种类型的消息有要从委托人传递给委托,委托可以通过为每个消息指定一个委托方法来使其更清晰。 对于通知,您可以使用多个通知名称,但所有通知的结束方式都与观察者相同(可能需要一个令人讨厌的切换语句)。



只有你可以决定什么模式更适合你。无论如何,您应该考虑不要让视图控制器发送通知或委托消息。在许多情况下,视图控制器应该更改模型,然后模型应通知其观察者或其代表它已被更改。



实现代理模式很简单:


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

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


  2. 在文件的顶部创建一个实例变量保持指向您的ChildViewController中的代理的指针:

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


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

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


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

      @implement RootViewController 
    ...
    //在某些方法中:
    ChildViewController * controller = [[ChildViewController alloc] initWithNibName:...
    controller.delegate = self;
    ...
    - (void)viewControllerDidChange:(ChildViewController *)controller {
    NSLog(@Delegate方法被调用。
    }
    ...


  5. 在ChildViewController实现中,调用代理方法在适当的时间:

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


就是这样(注意:我从记忆中写过,所以可能有一些打字错误)。


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).

Some advantages of delegating:

  • The connection between delegating object and delegate is made clearer, especially if implementing the delegate is mandatory.
  • If more than one type of message has to be passed from delegatee to delegate, delegating can make this clearer by specifying one delegate method per message. For notifications, you can use multiple notification names but all notifications end up in the same method on the side of the observer (possibly requiring a nasty switch statement).

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.

Implementing a delegate pattern is simple:

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

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

  2. 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
    

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

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

  4. 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.");
    }
    ...
    

  5. 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.)

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

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