Search code examples
xamarin.iosdesign-patternsobserver-patternnsnotificationcenter

Monotouch: Right manner for using NSNotificationCenter


What's the right manner to use NSNotificationCenter?

I would know if there is a sort of guideline to register and remove observers in a specific class.

NSNotificationCenter.DefaultCenter.AddObserver("NSString", Action)

is used to register an object as an obsever. How can I deregister an observer? Do I have to save all the observer for a specific class in arrays?

Thank you in advance. Regards.


Solution

  • The AddObserver method returns an object of type NSObject.

    So you call it like this:

    NSObject myObserver = NSNotificationCenter.DefaultCenter.AddObserver("NSString", Action);
    

    And when you want to remove it, call the following:

    NSNotificationCenter.DefaultCenter.RemoveObserver(myObserver);