Archive for January 2011
NSNotification Selectors and Subclasses
// Don’t do this…
@interface parentViewController : UIViewController
@end
@implementation parentViewController
- (void) viewDidLoad {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleTheNotification:) name:@”MyNotificationName” object:nil];
}
- (void)handleTheNotification:(NSNotification *)note
{
NSLog(@”Parent selector”);
}
@end
@interface childViewController : parentViewController
@end
@implementation childViewController
- (void)viewDidLoad {
// update from my original post – as brought up in the comments I do call super in viewDidLoad
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(HandleTheNotification:) name:@”MyNotificationName” object:nil];
}
- (void)handleTheNotification:(NSNotification *)note
{
NSLog(@”Override of parent selector”);
}
- (void)HandleTheNotification:(NSNotification *)note
{
NSLog(@”Stupid child notification that shouldn’t have been added”);
}
@end
// … the runtime will randomly call one or the other selector in you child class.