User Tools

Site Tools


ios-labs-s14:intermediate-notif

NSNotification

A method for sending app-wide messages to anything registered to hear them.

Any object can register for a notification by calling an Notification method for a given key. Any other object can post a notification, immediately alerting the first object of a given event.

Very useful for calling to objects that are unrelated to the sender, such as controllers in separate tabs of a TabBarController. Simultaneously extremely dangerous, since it creates code that is difficult to trace and catch errors from. To be used as a last resort for communicating information.

Registering for a notification:

[[NSNotificationCenter defaultCenter] addObserver:self
      selector:@selector(receiveTestNotification:) 
      name:@"TestNotification"
      object:nil];
  1. observer: the object to be notified
  2. selector: the method to be called when the notification comes in
  3. name: the key the notification is registered under; notifications with different keys will not be received
  4. object: some object passed with the notification

Posting a notification

[[NSNotificationCenter defaultCenter] 
      postNotificationName:@"TestNotification" 
      object:self];
ios-labs-s14/intermediate-notif.txt · Last modified: 2014/02/23 22:38 by mbarboi