• Home
  • Blog
  • iPhone
  • Exploring the Foundation Framework in iPhone- NS Notification Center

Exploring the Foundation Framework in iPhone- NS Notification Center

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
{
}
@end

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
{
}
@end

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]
                                                        fini

Math. h:-

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

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”);
}
@end

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

 

Frequently Asked iPhone Interview Questions & Answers

 

Job Support Program

Online Work Support for your on-job roles.

jobservice

Our work-support plans provide precise options as per your project tasks. Whether you are a newbie or an experienced professional seeking assistance in completing project tasks, we are here with the following plans to meet your custom needs:

  • Pay Per Hour
  • Pay Per Week
  • Monthly
Learn MoreGet Job Support
Course Schedule
NameDates
IOS Development TrainingNov 19 to Dec 04View Details
IOS Development TrainingNov 23 to Dec 08View Details
IOS Development TrainingNov 26 to Dec 11View Details
IOS Development TrainingNov 30 to Dec 15View Details
Last updated: 03 Apr 2023
About Author

Ravindra Savaram is a Technical 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.

read less