views:

327

answers:

2

How does inheritance of NSNotificationCenter observers work? I have a parent class that several other classes end up subclassing. The parent class registers itself as an observer for a specific notification. I was under the impression that children would also be registered as observers as long as you invoke the super method where the registration happens. I happen to put it into viewDidLoad.

Do I need to re-register child classes are observers also and re-implement their delegate methods?

+1  A: 

Each instance which calls -[super viewDidLoad] should end up receiving the notifications. You wouldn't need to override the notification-handling method as the superclass's implementation will be called. Of course if that's not sufficient in the subclass then you would have to provide an override.

Graham Lee
+4  A: 

You're registering objects, not classes, with notification centers. When you register for a notification, the callback method you register gets called on the object that registers, and if you've overridden it in a subclass, then the overridden method gets called.

If you're doing this in a subclass, ask yourself if the parent's code to register for the notification is getting called in the subclass.

This is all central to object-oriented programming and inheritance.

Jim Puls
I guess you _could_ register a class with a notification center, but that would be sort of scary…
Jonathan Sterling