iOS Guides | Samples

Foundation.NSNotificationCenter Class

A notification hub for the application.

See Also: NSNotificationCenter

Syntax

[Foundation.Register("NSNotificationCenter", true)]
public class NSNotificationCenter : NSObject

Remarks

The NSNotificationCenter is a hub that is used to listen to broadcast messages and post broadcast messages in an application. The messages that are posted are synchronous.

The NSNotificationCenter.DefaultCenter plays a central role in the application as this is where system notifications are posted for interesting system-level events.

Posting a notification is a synchronous process, which means that invoking one of the Post messages on the notification center will block execution until all of the notification handlers have completed running.

Notifications are posted on the same thread as the calling thread which could have undesired effects. If you need to deliver notifications from a background thread, use the NSObject.BeginInvokeOnMainThread(NSAction+action) method to wrap your posting into a delegate that is invoked on the main thread.

When subscribing to a notification, it is possible to subscribe only to a certain kind of notification by specifying the notification name (or null to listen to all notifications posted to the notification center). It is also possible to subscribe only to notifications issued by a specific object. This can be used to filter the amount of notifications received.

Notifications are usually NSString fields that are hosted in some class, for example the Objective-C UITextViewTextDidChangeNotification is found in the class UITextView as the UITextView.TextDidChangeNotification. For convenience there are overloads that take C# strings.

To register for notifications, you can use the AddObserver method. We recommend that you use the C# versions of these methods, as you can directly connect delegates, lambdas and methods directly into your code. When you use any of the AddObserver methods in this class that take an Action<NSNotification;> parameter, the AddObserver will return a token. This token is then used as the parameter to RemoveNotification when you want to stop listening to a particular event.

C# Example

NSObject notificationToken;

void Setup ()
{
	notificationToken = NSNotificationCenter.DefaultCenter.AddObserver (FooBar.ClockNotification, OnClockChange);
}

void OnClockChange (NSNotification notification)
{
	Console.WriteLine ("The ClockNotification message was posted");
}

void Teardown ()
{
	NSNotificationCenter.DefaultCenter.RemoveObserver (notificationToken);
}

Starting with MonoTouch 5.4, calling Dispose on the returned notification token will also remove the observer for you, making the code shorter. For example:

C# Example

NSObject notificationToken;

void Setup ()
{
	notificationToken = NSNotificationCenter.DefaultCenter.AddObserver (FooBar.ClockNotification, OnClockChange);
}

void OnClockChange (NSNotification notification)
{
	Console.WriteLine ("The ClockNotification message was posted");
}

void Teardown ()
{
	notificationToken.Dispose ();
}

Similarly, if your object implements IDisposable (including any object derived from NSObject) you MUST call NSNotificationCenter.RemoveObserver or notificationToken.Dispose() in your Dispose function:

C# Example

class MyClass : NSObject
{
      private NSObject notificationToken; 

      MyClass()
      { 
         notificationToken = NSNotificationCenter.DefaultCenter.AddObserver(notificationName, NotificationHandler);
      }

      void NotificationHandler(NSNotification notification)
      {
         // ... etc ...
      }

      private bool disposed = false;
      override protected void Dispose(bool disposing)
      {
         if (!disposed)
         {
               if (disposing)
               {
                  notificationToken.Dispose();
               }
               disposed = true;
               base.Dispose();
         }
      }
}

Related content

Requirements

Namespace: Foundation
Assembly: Xamarin.iOS (in Xamarin.iOS.dll)
Assembly Versions: 0.0.0.0

The members of Foundation.NSNotificationCenter are listed below.

See Also: NSObject

Public Constructors

Default constructor that initializes a new instance of this class with no parameters.

Protected Constructors

Constructor to call on derived classes to skip initialization and merely allocate the object.
A constructor used when creating managed representations of unmanaged objects; Called by the runtime.

Public Properties

[read-only]
override
ClassHandleIntPtr. The handle for this class.
[read-only]
static
DefaultCenterNSNotificationCenter. The default notification center, system notifications and system-level events are posted to this notification center.

Public Methods

AddObserver(NSString, Action<NSNotification>) : NSObject
Adds an observer for the specified notification
AddObserver(NSString, Action<NSNotification>, NSObject) : NSObject
Adds an observer for the specified notification
AddObserver(NSObject, Selector, NSString, NSObject)
Low-level add-bserver API
AddObserver(String, NSObject, NSOperationQueue, Action<NSNotification>) : NSObject
PostNotification(NSNotification)
Posts a notification that is broadcast to everyoned subscribing to that notification.
PostNotificationName(String, NSObject)
PostNotificationName(String, NSObject, NSDictionary)
RemoveObserver(NSObject)
RemoveObserver(NSObject, String, NSObject)
RemoveObservers(IEnumerable<NSObject>)
Removes multiple observers in one call.