Being a Cocoa developer, you are probably aware of delegates. Delegates are “helper objects” that run custom code when called by its object. For example, the class NSWindow has the delegate method windowDidMove:. If I have a Controller object with that method implemented and I set it to be a certain NSWindow’s delegate via [myWindow setDelegate:aController]; then whenever the window moves, my delegate object will be notified of it and run the code in the windowDidMove: method’s implementation.
- [NSNotificationCenter defaultCenter] is a shared instance.
- addObserver:self sets the current class to be the observer of defaultCenter. This is useful in a controller class, but self can be replaced to any other object that contains the method referred to in the next argument.
- selector:@selector(aWindowMoved:) is the name of the method within the observer that should be called when the notification (in the next argument) is sent out. Selector declarations should have something similar to (NSNotification *)aNotification as an argument, so aWindowMoved: is defined as -(void)aWindowMoved:(NSNotification *)aNotification; in the observer’s @implementation.
- name:NSWindowDidMoveNotification is the name of the notification. You can find these in the documentation for objects that have support for notifications
- object:nil says that any object that has an NSWindowDidMoveNotification notification can notify the observer. If you wanted to make this more delegate-like, though, you could specify an object, such as object:myWindow
- [[NSNotificationCenter defaultCenter[ removeObserver:self name:NSWindowDidMoveNotification object:nil]; removes self as the observer for all move notifications.
- [[NSNotificationCenter defaultCenter] removeObserver:self name:NSWindowDidMoveNotification object:myWindow]; removes self as the observer from move notifications from myWindow specifically.
- [[NSNotificationCenter defaultCenter] removeObserver:self name:nil object:myWindow]; removes self as the observer from all notifications from myWindow