NSNotificationCenter userInfo

I must be pretty daft to nearly lose my mind every time I want to use NSNotificationCenter on the iPhone… for uses beyond a basic method call someplace in the wild. I want to send information. I am posting this only because when it’s been a while since I’ve used them, and I seem to forget what I am doing and end up figuring it out all over again. Each time. So this post is reference for me.

If you haven’t used them before, this may help you along. This doesn’t send an object along, but it could (just change the nil to something useful for you).

In a class that is doing the yelling:

NSDictionary *dict;
dict = [NSDictionary dictionaryWithObjectsAndKeys:
yourStuff, nil];
[[NSNotificationCenter defaultCenter] postNotificationName:@”someString” object:nil userInfo:dict];

In the class doing the listening:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(someMethod: ) name:@”someString” object:nil];

– (void)
someMethod:(NSNotification *)notification {
NSDictionary *tmp = notification.userInfo;
//You could access notification.object here too

That’s about it really… stuff that userInfo with as much as you’d like within reason and have fun. NSNotificationCenter is really a fun way of yelling to whomever you want listening without worrying about scope. It’s quick and painless (unless like me – you had to track down using userInfo and/or object).

Happy Monday. Now, where is the nearest purveyor of hot liquid caffeine…

Related Posts Plugin for WordPress, Blogger...

Leave a Reply

Your email address will not be published. Required fields are marked *

eight × = 16

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>