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)
# import #import “second .h” @interface b8 notification view controller: UIviewController { } @end
-(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
# import # import “math .h” @ interface NSNotificationSecondViewController :UIViewController { } @end
# import “NSNotificationSecondviewController .h” @ implementation NSNotificationSecondviewController -(void) view Did load {
Add observer :self Selector : @ selector (addition); Name : @ “algebra notification” Object : nil]; [[NSNotificationCenter defaultCenter] add observer : self Selector :@ selector(subtraction) Name : @ “algebra notification” Object : nil];
Math *mathobj =[[math alloc]init]; [[NSNotification center default center] Add observer : mathobj Selector : @ selector (multiplication); Name : @ “algebra notification); Object : nil ];
[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
# import @interface Math : NSobject { } -(void) PostNotificationMathClass; @end
#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
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:
Name | Dates | |
---|---|---|
IOS Development Training | Nov 19 to Dec 04 | View Details |
IOS Development Training | Nov 23 to Dec 08 | View Details |
IOS Development Training | Nov 26 to Dec 11 | View Details |
IOS Development Training | Nov 30 to Dec 15 | View Details |
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.