Home  >  Blog  >   iPhone  > 

Exploring the Foundation Framework in iPhone- NS Notification Center

Rating: 4

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.

Inclined to build a profession as IOS Developer? Then here is the blog post on iPhone Development Certification Training.

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

MindMajix Youtube Channel

  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.


Frequently Asked iPhone Interview Questions & Answers


Join our newsletter

Stay updated with our newsletter, packed with Tutorials, Interview Questions, How-to's, Tips & Tricks, Latest Trends & Updates, and more ➤ Straight to your inbox!

Course Schedule
IOS Development TrainingJun 03 to Jun 18
IOS Development TrainingJun 06 to Jun 21
IOS Development TrainingJun 10 to Jun 25
IOS Development TrainingJun 13 to Jun 28
Last updated: 31 May 2023
About Author
Remy Sharp
Ravindra Savaram

Ravindra Savaram is a Content Lead at Mindmajix.com. His passion lies in writing articles on the most popular IT platforms including Machine learning, DevOps, Data Science, Artificial Intelligence, RPA, Deep Learning, and so on. You can stay up to date on all these technologies by following him on LinkedIn and Twitter.

Recommended Courses

1 /15