IOS development-message notification mechanism (Nsnotification and Nsnotificationcenter)

Source: Internet
Author: User

iOS in the delegation mode and the message mechanism is basically used in the development of more, the General Start page value through the delegation implementation of more, the value of the transfer between the class is more, but the delegate can only be a one-off, for example, page a jumps to page B, the value of the page B changes to map to page A, A change in the value of page C also needs to be mapped to page A, which requires two delegates to resolve the problem. Nsnotificaiton is a one-to-many registration notification, after which the callback is easy to solve the above problem.

Basic ConceptsThe IOS message notification mechanism is synchronous, and the observer simply registers with the message center to accept messages sent by other objects, and the message sender and message recipient can be completely decoupled from each other. This message notification mechanism can be applied to any time and any object, the observer can have more than one, so the message has the nature of broadcasting, but it is important to note that after registering with the message Center, the observer needs to log out of the message center when it is not necessary to accept the message, which belongs to the typical observer pattern.

Two important classes in the message notification:

(1) Nsnotificationcenter: The principle of realizing nsnotificationcenter is an observer pattern, there is only one way to obtain nsnotificationcenter, that is [ Nsnotificationcenter Defaultcenter], the object of this notification hub can be obtained by calling the static method Defaultcenter. Nsnotificationcenter is a singleton pattern, and the object of this notification center is always present in the life cycle of an application.

(2) Nsnotification: This is the carrier that the message carries, through it, can pass the message content to the observer.

Practical Walkthrough

1. Through Nsnotificationcenter registration notice the code in Nsnotification,viewdidload is as follows:

    [[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (notificationfirst:) name:@ "First" object: NIL];        [[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (notificationsecond:) name:@ "Second" Object:nil];

The first parameter is the observer for itself, the second parameter represents the method of the message callback, the name of the third message notification, and the fourth nil represents a message that accepts all senders ~

Callback method:

-(void) Notificationfirst: (nsnotification *) notification{    nsstring  *name=[notification name];    NSString  *object=[notification Object];    NSLog (@ "Name:%@----object:%@", name,object);} -(void) Notificationsecond: (nsnotification *) notification{    nsstring  *name=[notification name];    NSString  *object=[notification Object];    Nsdictionary  *dict=[notification UserInfo];    NSLog (@ "Name:%@----object:%@", name,object);    NSLog (@ "Get value:%@", [Dict objectforkey:@ "key"]);}

2. The message is passed to the viewer:

    [[Nsnotificationcenter Defaultcenter] postnotificationname:@ "first" object:@ "blog Park-fly_elephant"];        Nsdictionary  *dict=[[nsdictionary alloc]initwithobjects:@[@ "Keso"] forkeys:@[@ "key"];        [[Nsnotificationcenter Defaultcenter] postnotificationname:@ "Second" object:@ "Http://www.cnblogs.com/xiaofeixiang [Userinfo:dict];

3. Page Jump:

-(void) Pushcontroller: (UIButton *) sender{    viewcontroller *customcontroller=[[viewcontroller alloc]init];    [Self.navigationcontroller Pushviewcontroller:customcontroller animated:yes];}

4. Destruction of Observers

-(void) dealloc{    NSLog (@ "viewer destroyed");    [[Nsnotificationcenter Defaultcenter] removeobserver:self];}

You can also delete by name individually:

[[Nsnotificationcenter Defaultcenter] removeobserver:self name:@ "first" object:nil];

5. Running Results

2015-04-26 15:08:25.900 CUSTOALTERVIEW[2169:148380] The Observer destroyed 2015-04-26 15:08:29.222 custoalterview[2169:148380] Name: First----object: Blog Park-fly_elephant2015-04-26 15:08:29.222 custoalterview[2169:148380] Name: Second----Object:/HTTP Www.cnblogs.com/xiaofeixiang2015-04-26 15:08:29.223 custoalterview[2169:148380] Gets the value: Keso

IOS development-message notification mechanism (Nsnotification and Nsnotificationcenter)

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.