Nsnotificationcenter Message Communication mechanism Introduction (KVO) Role: Nsnotificationcenter is specially designed for communication between non-homogeneous messages in the program. Registration notice: Where to receive the message [[NSNotificationCenter Defaultcenter] addobserver:self selector: @selector (mytest:) name:@ " mytest" object:nil]; Parameter Introduction: addobserver: The Observer, where the notice is received; selector: What method to call after receiving notification; name: The name of the notification, is also the only indication of the notification, the compiler to find the notification through this. Send notification: Invokes the method at the Observer. [[nsnotificationcenter defaultcenter] postnotificationname:@ "MyTest" object:searchfriendarray]; parameters: postnotificationname: The name of the notice, is also the only indication of the notice, The compiler will find the notification through this. &Nbsp; object: Method of passing parameter registration:- (void) mytest :(nsnotification*) notification{ id obj = [notification object];// Get to the object passed} : Register the keyboard to restart the shutdown message//keyboard to rise [[nsnotificationcenter defaultcenter] addobserver:self selector: @selector (keyboardwillshow:) name:uikeyboardwillshownotification object:nil];//keyboard lowered [[ Nsnotificationcenter defaultcenter] addobserver:self selector: @selector (keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; See a program, inside Viewdidload there are nsnotificationcenter *center = [nsnotificationcenter defaultcenter ]; [[nsnotificationcenter defaultcenter] removeobserver:self name:@ " Savemessage " object:nil]; [center addobserver:self selector: @selector ( Savemessage) name:@"Savemessage" object:nil]; do not understand why first remove the registrant, and then add? Not the same observer? Message Delivery agency: For example, in a class that is needed, send a message//send a message out, here the object is an array:saveimagearray[[nsnotificationcenter defaultcenter] postnotificationname:@ "PostData" object:saveImageArray]; all friends and family give me a package (send message),,,//register a observer to respond to the delivery of the message [[ nsnotificationcenter defaultcenter] addobserver:self selector: @selector (postimage:)//Receive Message Method name:@ "PostData"//Message recognition name       &Nbsp; object:nil]; For example, the Chinese New Year, prepare a big wallet (register a observer), hehe,,,, //Implementation Method -(void) Postimage: (nsarray *) array{ //receive messages sent over} I put the red envelopes away, (receive the message) //removing observer [[NSNotificationCenter defaultcenter] removeobserver:self name:@ "PostData" object:nil]; red envelopes are finished, haha, friends and family home. Simple explanation, haha, do not need to understand too complex .... Example: KVC used to send messages, is very good. KVO registration Take Parserdatas as an example: Parserdatas is a class that parses XML [parserdatas addobserver:self forkeypath:@ "Isfinished" options: (nskeyvalueobservingoptionnew | nskeyvalueobservingoptionold) context:parserdatas];//Receive change notification- (void) Observevalueforkeypath: (nsstring *) keypath ofobject: (ID) object change: (nsdictionary *) change context: (void *) context{ if ([KeyPath isequal:@ "isfinished"]) { bool isfinished=[[ change objectforkey:nskeyvaluechangenewkey] intvalue]; if (isfinished) {//if server data is received nsmutablearray *array = [[NSMutableArray alloc] init]; [array addObjectsFromArray:parserDatas.parsedDataArray]; //Save Data [[UIApplication sharedapplication] setnetworkactivityindicatorvisible:no]; NSString *path = [DOCUMENT_PATH stringbyappendingpathcomponent:@ "Allxmldataarray.bin"]; [NSKeyedArchiver archiveRootObject:array toFile:path]; [array release]; //Cancel KVO Registration [object removeObserver:self forkeypath:@ "Isfinished"]; } &Nbsp; }else{ // be sure to call the super implementation // if the superclass implements it [super observevalueforkeypath:keypath ofobject:o bject change:change context:context]; }} Define the:+ (BOOL):(in the Parserdatas class Nsstring*) key{ //When these two values change, using automatic notification of registered observers, the observer needs to implement ObserveValueForKeyPath:ofObject:change: Context: Method if ([key isequaltostring:@ "isfinished"]) { return YES; } return [super automaticallynotifiesobserversforkey:key];} Send change Notification: add: [self setvalue:[nsnumber numberwithbool:yes] forkey:@ "isfinished"] in the function that needs to trace the message record; [[nsnotificationcenter defaultcenter] removeobserver:self name:@ " Savemessage " object:nil]; no time, just write off the. Usually put in dealloc .... Inside. Before using the notification register, is to prevent multiple registrations, this is generally because the view in the program to load a lot of times, such as the Three20 of the non-top view every time the display is re-load, This way, if you do not register the notification before use, it will repeat the registration of the message, such as every time you receive a message to print a word, when you open the view of n times, this sentence will print n times ...- (void) awakefromnib{[[ Nsnotificationcenter defaultcenter] addobserver:selfselector: @selector (switchviews:) name:@ " Switchviews " object:nil];} - (void) Switchviews: (nsnotification*) notification{nsnumber *viewnumber = [ notification object]; nsinteger i = [viewnumber integervalue]; [self setselectedsegmentindex:i]; uiview *chosenview = nil;switch (i) {case 0:chosenView = view1;break; Case 1:chosenview = view2;break;case 2:chosenview = view3;break;default:break;} if (Chosenview) {[[viewcontroller view] bringsubviewtofront:chosenview];} - (void) dealloc{[super dealloc];[ [Nsnotificationcenter defaultcenter] removeobserver:self];} @end
Introduction to the Nsnotificationcenter message communication mechanism (KVO)