Foundation.NSNotificationCenter Class
A notification hub for the application.

See Also: NSNotificationCenter Members

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() 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 Objetive-C UITextViewTextDidChangeNotification is found in the class UITextView as the UIKit.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 ();
}

Related content

Requirements

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