Nick Harris

Archive for January 2011

NSNotification Selectors and Subclasses

with 3 comments

// 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.

Advertisements

Written by Nick Harris

January 5, 2011 at 3:06 am

Posted in Uncategorized