How can we send a message to a function when some event happen? For example, today I was writing a function which download an XML file and when it finish send a notification to another function indicating "hey! do something, I've finished my job".
Well, it's very easy:
1) Add an observer to the defaultCenter
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(selectorDoSomething) name:@"DoSomething" object:nil];
...
-(void) SelectorDoSomething {
// Code here
}
...
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}
2) Post the notification from other function (or class)
[[NSNotificationCenter defaultCenter] postNotificationName:@"DoSomething" object:self userInfo:nil];
And... done! You have your notifications working.
Hope help
No comments:
Post a Comment