What is it?

[Apple Doc] 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.

When to use it?

When you need to broadcast(for example) a completion of a block across a few view controllers. It is known as observer pattern.

You booked something online and the company will notify you when the postage reached your nearest post office. The Post office that notified you is the notification center and you are the object waiting to be notified.

How to use it?

//1. Setup your notification to notify
NSString *notificationName = @"YourNotificationName";
NSString *key = @"ObjectKey";
NSString *object = @"ObjectContent";
NSDictionary *dictionary = [NSDictionary dictionaryWithObject:object forKey:key];
[[NSNotificationCenter defaultCenter] postNotificationName:notificationName object:nil userInfo:dictionary];
//2. Wait to be notified - with matching notification name
    [[NSNotificationCenter defaultCenter]

- (void)activateAMethod:(NSString *)stringName{
    //3. A method that activated to carry out

For memory management purposes, you need to 
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];

For Other App NSNotifications

You can use nsnotification to notify other parts of your app for your app state:

  • UIApplicationDidBecomeActiveNotification
  • UIApplicationDidChangeStatusBarOrientationNotification
  • UIApplicationDidEnterBackgroundNotification
  • UIApplicationDidFinishLaunchingNotification
  • UIApplicationDidReceiveMemoryWarningNotification
  • UIApplicationProtectedDataDidBecomeAvailable
  • UIApplicationProtectedDataWillBecomeUnavailable
  • UIApplicationSignificantTimeChangeNotification
  • UIApplicationUserDidTakeScreenshotNotification
  • UIApplicationWillChangeStatusBarOrientationNotification
  • UIApplicationWillChangeStatusBarFrameNotification
  • UIApplicationWillEnterForegroundNotification
  • UIApplicationWillResignActiveNotification
  • UIApplicationWillTerminateNotification
  • UIContentSizeCategoryDidChangeNotification and much more…

For example:

[[NSNotificationCenter defaultCenter] addObserver:self
                                             name:UIApplicationDidBecomeActiveNotification object:nil];

Delegates or NSNotificationCenter?

From the source here
The rule of thumb here is how many clients would like to be notified of an event. If it’s mainly one object (e.g. to dismiss a view or to act upon a button clicked, or to react to a failed download) then you should use the delegate model.

If the event you emit may be of an interest to many objects at once (e.g. screen rotated, memory usage, user login/logout), then you should use the NSNotificationCenter.



For my small workaround, I’ll compare to see if my working flow is similar as UITableView (which repeatedly notify the object according to app interaction)

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

If not, then will my working flow similar as my postage online example(see above)? If yes, NSNotificationCenter is the choice.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s