SlideShare a Scribd company logo
NSNotification
• NSNotification

•
    •
    •
    •
•
•   NSNotification

•         NSNotificationCenter



•
•                                           /



                            MyObjB


                                                NSNotification


    MyObjA            NSNotificationCenter                       MySendObj




                            MyObjC
•
•

•
                  MyObjB       “Hoge”


                                            “Hoge”


    MyObjA   NSNotificationCenter                     MySendObj




                  MyObjC
                                        ”foo”
•                                NSNotificationCenter

    •               /

•        ”      ”

    •
•            NSNotification            userInfo(NSDictionary) release


                             MyObjB




    MyObjA              NSNotificationCenter                            MySendObj




                             MyObjC
•
•
•
•
MyObjB                   ”hoge”                                                 popupAlert



 	   [[NSNotificationCenter defaultCenter] addObserver:self
 	   	     	    	    	    	    	    	    	    	    	      selector:@selector(popupAlert:)
 	   	     	    	    	    	    	    	    	    	    	    	      name:@”hoge”
 	   	     	    	    	    	    	    	    	    	    	        object:nil];




                                       MyObjB


                                                   addObserver
                             NSNotificationCenter
MySendObj                                ”hoge”
	   // NSNotification
    NSNotification* notification = [NSNotification notificationWithName: @”hoge”
	   	     	    	    	    	    	    	    	    	    	    	     object:self userInfo:nil];
	
    //
    [[NSNotificationCenter defaultCenter] postNotification:notification];




                                                                            “Hoge”


    NSNotificationCenter                                                                   MySendObj
                                           postNotification
NSNotificationCenter                                  MyObjB
                 popupAlert
  -(void)popupAlert:(NSNotification *)notification   {
  	   //
  	   UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Sample"
  	   	     	    	    	    	    	    	    	    	    	    	    	    message:@”       ”
  	   	     	    	       	   	     	   	     	   	   	      	       delegate:self
  	   	     	    	       	   	     	   	     	   	       cancelButtonTitle:@"OK"
  	   	     	    	       	   	     	   	     	   	       otherButtonTitles: nil];
  	
  	   [alert show];
  	   [alert release];
  }




                                           MyObjB

                                                              “Hoge”



                                 NSNotificationCenter
removeObserver

- (void)dealloc {
	   //                             Notification
	   [[NSNotificationCenter defaultCenter] removeObserver:self];
}




                             MyObjB


                                     removeObserver
                    NSNotificationCenter

More Related Content

iPhone NSNotification

  • 2. • NSNotification • • • • •
  • 3. NSNotification • NSNotificationCenter • • / MyObjB NSNotification MyObjA NSNotificationCenter MySendObj MyObjC
  • 4. • • • MyObjB “Hoge” “Hoge” MyObjA NSNotificationCenter MySendObj MyObjC ”foo”
  • 5. NSNotificationCenter • / • ” ” • • NSNotification userInfo(NSDictionary) release MyObjB MyObjA NSNotificationCenter MySendObj MyObjC
  • 7. MyObjB ”hoge” popupAlert [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(popupAlert:) name:@”hoge” object:nil]; MyObjB addObserver NSNotificationCenter
  • 8. MySendObj ”hoge” // NSNotification NSNotification* notification = [NSNotification notificationWithName: @”hoge” object:self userInfo:nil]; // [[NSNotificationCenter defaultCenter] postNotification:notification]; “Hoge” NSNotificationCenter MySendObj postNotification
  • 9. NSNotificationCenter MyObjB popupAlert -(void)popupAlert:(NSNotification *)notification { // UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Sample" message:@” ” delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil]; [alert show]; [alert release]; } MyObjB “Hoge” NSNotificationCenter
  • 10. removeObserver - (void)dealloc { // Notification [[NSNotificationCenter defaultCenter] removeObserver:self]; } MyObjB removeObserver NSNotificationCenter