kwigbo

Adventures in iOS!

0 notes &

NSNotification Management

Coming from the world of Flash/Actionscript I really like the concept of NSNotifications in iPhone apps. Problem is, keeping track of your notifications can become a pain real quick. That is why I like to create a centralized location to manage all my NSNotifications. More after the jump.

First we will create our header. (Read comments)

@interface ApplicationNotifications : NSObject
{
}

// This will be our constants for our first notifications
extern NSString *const MY_FIRST_NOTIFICATION;
extern NSString *const MY_FIRST_NOTIFICATION_WITH_STRING;

// We will call these to post our notifications
+ (void) postMyFirstNotification;
+ (void) postMyFirstNotificationWithString:(NSString *) string;

// These will be called to allow objects to listen to and 
// stop listening for notifications
// These are just a convenience and not required
+ (void) addObserver:(id) observer selector:(SEL) 
		selector name:(NSString *) name;

+ (void)removeObserver:(id)notificationObserver 
		name:(NSString *)notificationName;

+ (void) removeObserver:(id) observer;

@end

Next we will write our implementation. (Read comments)

#import "ApplicationNotifications.h"

@implementation ApplicationNotifications

// Here we define the values for our constants
NSString *const MY_FIRST_NOTIFICATION = @"MyFirstNote";
NSString *const MY_FIRST_NOTIFICATION_WITH_STRING = @"MyFirstNoteWithString";

// Here we just call the defaultCenter and post a 
// notification with our constant name.
+ (void) postMyFirstNotification
{
	[[NSNotificationCenter defaultCenter] 
		postNotificationName:MY_FIRST_NOTIFICATION object:nil];
}

// Same as the previous method, we just pass an 
// NSString along with the notification.
+ (void) postMyFirstNotificationWithString:(NSString *) string
{
       [[NSNotificationCenter defaultCenter] 
		postNotificationName:MY_FIRST_NOTIFICATION object:string];
}

// Here are our convenience methods for adding and removing observers
+ (void) addObserver:(id) observer selector:(SEL) selector 
		name:(NSString *) name
{
	[[NSNotificationCenter defaultCenter] addObserver:observer 
		selector:selector name:name object:nil];
}

+ (void)removeObserver:(id)notificationObserver 
	name:(NSString *)notificationName
{
	[[NSNotificationCenter defaultCenter] removeObserver:notificationObserver 
		name:notificationName object:notificationSender];
}

+ (void) removeObserver:(id) observer
{
	[[NSNotificationCenter defaultCenter] removeObserver:observer];
}

@end

When you want to post one of your notifications it’s as simple as importing the ApplicationNotifications class and calling one of our post methods.

[ApplicationNotifications postMyFirstNotification];
[ApplicationNotifications postMyFirstNotificationWithString:@"mystring"];

How do we listen for these notifications? All we need to do is import our ApplicationNotifications class and call the addObserver method

[ApplicationNotifications addObserver:self selector:@selector(myFirstNote) 
		name:MY_FIRST_NOTIFICATION];

// Notice the : in the selector in this one. We need this to get the NSNotification
// object to grab our string from.
[ApplicationNotifications addObserver:self selector:@selector(myFirstNoteWithString:) 
		name:MY_FIRST_NOTIFICATION_WITH_STRING];

Next we setup our methods that will be called. These will match the selectors specified in the last step.

- (void) myFirstNote
{
    // handle notification here
}

- (void) myFirstNoteWithString:(NSNotification *) note
{
    // Here we grab our string from the notification. 
    // we could also pass any other custom
    // object along with the notification.
    NSString *stringFromNote = (NSString *)[note object];
}

We alos need to clean up our notifications when we don’t need them anymore. We can either clean them up one at a time when not needed or all at once for an individual observer.

// This is good to call in the dealloc method to 
// stop the object from observing all notes
[ApplicationNotifications removeObserver:self];
// This one is used if at some point in the life of our application we just want
// to stop listening for a single notification
[ApplicationNotifications removeObserver:self name:MY_FIRST_NOTIFICATION];

When it comes down to it the most convenient part of this is when dispatching NSNotification. The rest of it is just there for consistency. As always if there is any confusion please ask questions in the comments section below. Thanks!

Filed under tutorials