01-09 Notice

Source: Internet
Author: User

Notification hubs in iOS Nsnotificationcenter application Summary One, learn about several related classes 1, nsnotification

This class can be understood as a message object with three member variables.

This member variable is a unique identifier for this message object and is used to identify the message object.

@property (readonly, copy) NSString *name;

This member variable defines an object that can be understood as a message against an object.

@property (ReadOnly, retain) ID object;

This member variable is a dictionary that can be used to pass values.

@property (readonly, copy) Nsdictionary *userinfo;

Initialization method for Nsnotification:

-(Instancetype) Initwithname: (NSString *) Name object: (ID) object userInfo: (Nsdictionary *) UserInfo;

+ (Instancetype) Notificationwithname: (NSString *) AName object: (ID) anobject;

+ (Instancetype) Notificationwithname: (NSString *) AName object: (ID) anobject userInfo: (nsdictionary *) Auserinfo;

Note: The official documentation is explicitly stated and cannot be initialized with Init

2, Nsnotificationcenter

This class is a notification hub that uses a singleton design and each application has a default notification hub. The acceptance of the send that is used to dispatch notifications.

Add an observer that can specify a method, a name, and an object for it. Executes the method when the notification is accepted.

-(void) Addobserver: (ID) Observer selector: (SEL) aselector name: (NSString *) AName object: (ID) anobject;

Ways to send notification messages

-(void) Postnotification: (nsnotification *) notification;

-(void) Postnotificationname: (NSString *) AName object: (ID) anobject;

-(void) Postnotificationname: (NSString *) AName object: (ID) anobject userInfo: (nsdictionary *) Auserinfo;

Ways to remove observers

-(void) Removeobserver: (ID) observer;

-(void) Removeobserver: (ID) Observer name: (NSString *) AName object: (ID) anobject;

Some notes:

1. If a notification is sent specifying an object, then the object of the notification set by the observer is the same as that of the object, but the notification is received if the parameter is set to nil.

2, the Observer's SEL function pointer can have a parameter, the parameter is sent by the dead Ossicini object itself, you can take this parameter to the Message object UserInfo, to achieve the value.

Second, the use of the notification process

First, we register observers where we need to receive notifications, such as:

?
1234     //获取通知中心单例对象    NSNotificationCenter * center = [NSNotificationCenter defaultCenter];    //添加当前类对象为一个观察者,name和object设置为nil,表示接收一切通知    [center addObserver:self selector:@selector(notice:) name:@"123"object:nil];

After that, send a notification message when we need it

?
1234     //创建一个消息对象    NSNotification * notice = [NSNotification notificationWithName:@"123"object:nil userInfo:@{@"1":@"123"}];    //发送消息       [[NSNotificationCenter defaultCenter]postNotification:notice];

We can fetch the userinfo content in the callback function, as follows:

?
123 -(void)notice:(id)sender{    NSLog(@"%@",sender);}

The printing results are as follows:

01-09 Notice

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.