iPhone : Exploring the Foundation Framework – NS Notification Centre

NSNotification Center:-

An NSNotificationCenter object (or simply, notification center) provides a mechanism for broadcasting information within a program. An NSNotificationCenter object is essentially a notification dispatch table.

OBJECTS register with a notification center to receive notifications (NSNotification objects) using the addObserver:selector:name:object: or addObserverForName:object:queue:usingBlock: methods. Each invocation of this method specifies a set of notifications. Therefore, objects may register as observers of different notification sets by calling these methods several times.

Each running Cocoa program has a default notification center. You typically don’t create your own. An NSNotificationCenter object can deliver notifications only within a single program. If you want to post a notification to other processes or receive notifications from other processes, use an instance of NSDistributedNotificationCenter.

         Initially create the file name (b8 notification view controller)

b8 notification view controller .h:

# import  
#import     “second .h”
@interface b8 notification view controller: UIviewController

b8 notification view controller .m:

-(void)viewDid load
[[NSNotificationCenter defaultCenter] add observe : selfSelector:@ selector :@ selector (addition) name :@ “ AlgebraNotification” Object
: nil];
[[NSNotificationCenter  defaultCenter ]
                                              addobserver : self
                                              Selector :@ selector (subtraction)
                                                  name :@ “algebra notification”
                                                   Object  : nil];
[[NSNotificationCenter defaultCenter
          PostNotificationName: @ “AlgebraNotification”
                                                             Object: nil];
     [ Super  viewDidLoad];
-(void) addition
NSLog(@ “ the addition is called”)
-(void) Subtraction
Ns log(@ “the subtraction is called);

Output:– the addition is called

                  The subtraction is called

program for the notification center to create an another class

  NSNotificationSecondViewController. h:-

  # import 
# import         “math .h”
   @ interface NSNotificationSecondViewController :UIViewController

NSNotificationSecondViewController .m:-

   # import “NSNotificationSecondviewController .h”
   @ implementation NSNotificationSecondviewController
-(void) view Did load

[[NSNotificationCenter defaultCenter]

  Add observer :self
     Selector      :  @  selector (addition);

 Name      :    @ “algebra notification”
   Object       : nil];

[[NSNotificationCenter defaultCenter]

     add observer  : self
    Selector      :@ selector(subtraction)
       Name       : @ “algebra notification”
        Object       :  nil];

//Create an object to math class:

  Math     *mathobj   =[[math alloc]init];
[[NSNotification center default center]
                                             Add observer    :  mathobj
                                             Selector           :  @ selector  (multiplication);
                                             Name               : @  “algebra notification);
                                              Object             : nil ];

// post notification math class method:

  [Math obj   postNotificationMathclass]
[Super view did load];
-(void)  addition : (NSNotification    *)  aNotification
NSDictionary   * aDict   = [a Notification  userInfo];
NSLog   (@  “addition is called %@”, aDict ];
-(void) subtraction  :( NSNotification *)  aNotification
Ns dictionary     *a dict   =   [ a notification   user  info};
NSLog  (@ “ subtraction  is called  %  @”, aDict);
à//by orating the class for math
   Class àaddànew fileàcocaa touch class
                                                    Objective-c class     
                                                  Next click                          
                                            File name :math .m
                                                                  Math. h  [automatically create . h]

Math. h:-

# import    
@interface  Math : NSobject
-(void)  PostNotificationMathClass;

Math .m:-

#import “Math .h”
@implementation Math
- (void) PostNotificationMathClass
NSDictionary * aDict  =
[NSDictionary dictionarywithObjectsAndkeys: @ “ vamshi”, @ “divya”,  nil];
[[NSNotificationCenter defaultCenter]
     PostNotificatioName:@ “AlgebraNotification”
                                      Object : nil
                            User info : aDict];
-(void) multiplication
NSLog  (@ “multiplication is called”);

Output:–         addition is called {
                          Divya  =  vamshi;
                        Subtraction is called 
                               Divya   =   vamshi;
                                 Multiplication is called.


Get Updates on Tech posts, Interview & Certification questions and training schedules