Qf--ios Messaging mechanism (notification hubs)

Source: Internet
Author: User

Before we talked about iOS different interface between the value of the time, said can be sent through the notification center value. So what is the notification center, and how did he achieve the value of the transfer?

Nsnotificationcenter is a singleton and only provides a unique instantiation of the portal, which only has one instance within the entire application: [nsnotificationcenter defaultcenter]

You can automatically receive messages from other objects through the notification hub as long as you follow the Nsnotificationcenter instance. Because its followers can have many, so it also has a broadcast nature.

In the notification center, the message sender and receiver are completely decoupled , which is excellent.

The essence of notification hubs is the observer pattern, which registers an object (Nsnotificationcenter) for observation, triggering a callback method whenever it changes.

To use notification hubs: Suppose you want to pass a value from a to B

1.B focus on the notification center, you can also say to the Notification Center to register Observer B:

[[nsnotificationcenter defaultcenter] addobserver:self selector:@selector( Urlchange:) name:@ "Newurl" object:nil];

< Span class= "S1" > 2.A send message via notification hub:

< Span class= "S1" > < Span class= "S1" >   [[nsnotificationcenter Defaultcenter Postnotificationname:@ "Newurl" Object:nil Userinfo:@{@ "Newsiteurl" :value};

3. Because B is concerned with notification hubs, the callback method is triggered when the notification hub has a message, so execution -(void) Urlchange: (nsnotification *) Notification

Note: Nsnotification is the carrier of the message information, the Notification Center can be obtained through the Notification.userinfo notice information.

4. Remove the viewer:

Overriding destructor Dealloc, removing the viewer in it (to cancel the attention to the Notification center)

[[nsnotificationcenter defaultcenter] removeobserver:self];

    

Qf--ios Messaging mechanism (notification hubs)

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.