Agents and Notifications

Source: Internet
Author: User
Tags notification center

Procedures for using agents and notification agents
    • Define an agent Agreement
      • The format of the protocol name is generally: class name + Delegate
        • Like Uitableviewdelegate.
      • Proxy method Details
        • It's usually @optional.
        • Method names usually start with the class name
          • Like what- (void)scrollViewDidScroll:
        • It's usually necessary to pass the object itself out.
          • For example, TableView's method will pass the tableview itself out.
      • Must adhere to the NSObject agreement
        • Like what@protocol XMGWineCellDelegate <NSObject>
    • Declaring a proxy property
      • Type of proxy format:id< Protocol > delegate
@property (nonatomic, weak) id<XMGWineCellDelegate> delegate;
    • Set proxy Object
xxx.delegate = yyy;
    • The proxy object adheres to the protocol and implements the corresponding method within the protocol.

    • When something happens inside the control, you can call the agent's proxy method to notify the agent

      • If the proxy method is @optional, then you need to determine if the method is implemented
if ([self.delegate respondsToSelector:@selector(wineCellDidClickPlusButton:)]) {    [self.delegate wineCellDidClickPlusButton:self];}
Notice
    • Announcement of the notification (publisher)
    • Monitoring of notifications (listeners)
    • Removal of notifications
Notification Center (Nsnottificationcenter)
    • Each application has a notification hubs (Nsnotificationcenter) instance that is specifically designed to assist message communication between different objects
    • any object can publish notifications (nsnotification) to the notification hub, Describe what you are doing. Other objects of interest (Observer) can request to receive this notification when a particular notification is published (or when a specific object is published)
//a complete notification generally contains 3 attributes:-(nsstring *) name; //Notification name-(id) object; //notifies the publisher (who wants to publish the notice)-(nsdictionary *) userInfo; //Some additional information (informing the publisher of the content delivered to the notification recipient) //initialize a notification (nsnotification) object + ( Instancetype) Notificationwithname: (nsstring *) AName object: ( id) anobject;+ (instancetype) Notificationwithname: ( NSString *) AName object: (id) anobject userInfo: ( Nsdictionary *) auserinfo;-(instancetype) Initwithname: (nsstring *) Name object: (id) object UserInfo: (nsdictionary *) userInfo;     
Publish Notifications
- (void)postNotification:(NSNotification *)notification;发布一个notification通知,可在notification对象中设置通知的名称、通知发布者、额外信息等- (void)postNotificationName:(NSString *)aName object:(id)anObject;发布一个名称为aName的通知,anObject为这个通知的发布者- (void)postNotificationName:(NSString *)aName object:(id)anObject userInfo:(NSDictionary *)aUserInfo;发布一个名称为aName的通知,anObject为这个通知的发布者,aUserInfo为额外信息
Registering a notification listener
/*observer:监听器,即谁要接收这个通知aSelector:收到通知后,回调监听器的这个方法,并且把通知对象当做参数传入aName:通知的名称。如果为nil,那么无论通知的名称是什么,监听器都能收到这个通知anObject:通知发布者。如果为anObject和aName都为nil,监听器都收到所有的通知*/- (void)addObserver:(id)observer selector:(SEL)aSelector name:(NSString *)aName object:(id)anObject;
Unregister Notification Listener
    • Notification hubs does not hold (retain) listener objects, and objects registered in the notification hub must be unregistered before the object is released. Otherwise, the notification hub will still send a message to the listener when the corresponding notification appears again. The application may crash because the listener object has been freed

    • Notification hubs provides the appropriate method to unregister listeners

- (void)removeObserver:(id)observer;- (void)removeObserver:(id)observer name:(NSString *)aName object:(id)anObject;//一般在监听器销毁之前取消注册(如在监听器中加入下列代码):- (void)dealloc { //[super dealloc]; 非ARC中需要调用此句 [[NSNotificationCenter defaultCenter] removeObserver:self];}
Example of a notification (code)
    • AAA is to inform the issuer that AAA is the listener
//aaa监听AAA的<军事新闻>通知[[NSNotificationCenter defaultCenter] addObserver:aaa selector:@selector(gotNews:) name:@"军事新闻" object:AAA];//AAA发布<军事新闻通知>[[NSNotificationCenter defaultCenter] postNotificationName:@"军事新闻" object:AAA userInfo:@{@"title" : @"453543"}];//销毁aaa监听者[[NSNotificationCenter defaultCenter] removeObserver:aaa];
How iOS listens to certain events
    • Notice (nsnotificationcenter\nsnotification)
      • Messages can be passed between any object
      • Scope of Use
        • 1 objects can be notified to n objects
        • 1 objects can accept notifications from n objects
      • Must be guaranteed that the name of the notification is consistent when it is issued and monitored.
    • KVO
      • Only the ability to listen to changes in object properties (less flexible than notifications and proxies)
    • Agent
      • Scope of Use
        • 1 objects can only be set one proxy (assuming that the object has only 1 proxy properties)
        • 1 objects can be proxies for multiple objects
      • than the 通知 specification
      • It is recommended to use 代理 more通知

Agents and Notifications

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.