IOS notification mechanism, ios notification

Source: Internet
Author: User
Tags notification center

IOS notification mechanism, ios notification

On the Internet, it is often said that the iOS notification mechanism uses the observer mode, which has two roles: poster (sender) and observer (subscriber that accepts information ). However, I think the important role is the notification center, which is the core of the entire notification mechanism. messages sent by poster Senders must arrive at the notification center, the notification center then distributes messages to those subscribers based on which observer subscribers subscribe to the message. The overall structure can be analogous to the current email structure. However, it should be noted that although iOS notifications are also called Notification notifications, they are different from notifications in Android. notifications in Android are a form of message push, in iOS, there must also be message push, which is the part of the push mechanism. The notification mechanism is actually an event that was originally learned in C #, but the event here is a global event for the entire system, any party registers and binds a method to the global event of the system, and the method can be executed when the event is triggered.

For the entire notification operation process, we only need to set poster and observer.

ForPosterThe operation is relatively simple, he only needs to push the notification to the notification center, use the following code

[[NSNotificationCenter defaultCenter] postNotificationName:@”PostOne” object:@”This is posterone”];

Or

[[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:@”PostOne” object:@”This is posterone”]];

Icationicationwithname: object: Another overload of the method is the userInfo: parameter, which is a dictionary type of NSDictionary and is used to pass user parameters.

ForObserverIt is relatively less simple than Poster. When binding event registration in C #, we also found that the trigger event only needs to be called like the call method, however, to register that party, you need to bind the method to the event and define the method. In the iOS notification mechanism, You need to register and handle the callback (that is, the method definition in the event ), you have to delete it when it is used up.

Register
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(callBack1:) name:@”PostOne” object: nil];

Here, the parameter corresponds to postNotificationName: object. Enter the corresponding value for the notification name parameter you have registered.

Callback Processing
-(Void) callBack1 (NSNotification *) notification {notification. name: // notification name. object; // The object notification when the notification is sent. userInfo // userInfo when the notification is sent}
Delete

After a message is used (that is, the received message is no longer processed), delete the message.

[[Nsicationcenter center defacenter center] removeObserver: self]; // delete all registered notifications [[nsicationicationcenter defaultCenter] removeObserver: self name: @ "PostOne" object: nil]; // delete a notification named "PostOne.

 

Related Article

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.