IOS about Nsnotificationcenter

Source: Internet
Author: User

What we usually do when we do something in IOS is implemented by Delegate, and Apple gives us another way to respond to notifications, which is nsnotification.

Nsnotificationcenter can achieve a greater span of communication mechanisms than Delegate, which can communicate with two non-referencing two objects. The Nsnotificationcenter communication principle uses the Observer pattern (KVO): Three Step 1 Register 2 where it is needed to implement and write a trigger event 3 to fire where the event needs to be triggered

1 . Nsnotificationcenter registered observers are interested in an event (named after a string) and the Selector or Block 2 that executes when the event is triggered. Nsnotificationcenter fires an event (named String) 3 at a certain time. The observer performs the corresponding Selector or Block when receiving an event of interest
4. Cancellation of the Observer this corresponds to the registration is generally in the selector method, also relative to the time of registration is generally viewwillappear registration Viewwilldisapper logoff
////SECONDVIEWCONTROLLER.M//StoryBoard////Created by HF on 15/1/12.//Copyright (c) 2015 ylyl. All rights reserved.//#import"SecondViewController.h"#import"BasicFunctuin.h"@interface Secondviewcontroller () @end @implementation Secondviewcontroller- (void) viewdidload {[Super viewdidload]; Registration Notification Notification Implementation method is the update activation keyword is @ "update"[[Nsnotificationcenter defaultcenter] addobserver:self selector: @selector (UPDATE) Name:@"Update" Object: nil];}
Trigger Method-(void) update{DLog (@"KVO");

Observer logoff, removing message Viewer

[[Nsnotificationcenter defaultcenter]removeobserver:self name:@ "Update" Object:nil];

} @end
////FIRSTVIEWCONTROLLER.M//StoryBoard////Created by Wzyb on 14-12-11.//Copyright (c) 2014 ylyl. All rights reserved.//#import"FirstViewController.h"@interface Firstviewcontroller () @end @implementation Firstviewcontroller- (void) viewdidload {[Super viewdidload];}-(void) Viewwillappear: (BOOL) animated{[Super Viewwillappear:yes];
The notification method that implements notification at this point that triggers the keyword @ "update" as long as the keyword method is executed [[Nsnotificationcenter Defaultcenter] Postnotificationname:@"Update" Object: nil];} @end
Although it is not an error to remove Nsnotification Observer after using ARC on IOS, it is a bad habit that is not conducive to performance and memory.

Remove Notifications: Removeobserver: And RemoveObserver:name:object: Where, Removeobserver: is the delete Notification Center Save Schedule table for all the portals of an observer, and Removeobserver: Name:object: is to delete a portal that matches the observer in the dispatch table saved by the notification center.

This is relatively straightforward, just call the method on the line. For example:

[[Nsnotificationcenter Defaultcenter] removeobserver:observer name:nil object:self];

Note that the parameter notificationobserver is the observer to be deleted and must not be nil.

There are 2 ways to unregister an observer: A. The optimal method, in UIVIEWCONTROLLER.M:-(void) dealloc {[[Nsnotificationcenter Defaultcenter] removeobserver:self];}
Now in Arc General post B. Single remove: [[nsnotificationcenter Defaultcenter] removeobserver:self name:@ " Notification_getuserprofilesuccess"object: nil";

IOS about 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.