Jul 15

Some time ago I commented how to detect changes in properties using KVO (Key-Value Observing), today we will see how to do the same using notifications.

The idea from the example is the following: We have an object A from a X class, we want to be notified when a property of A is changed, to be so, we will notify another object B, so B can do some stuff. We are going to do this with notifications, the object A will send a notification when it changes it’s property and the object B will be looking for this notifications.

Lets see what do we need to do in the code for that, first we create a function which will send the notification. We are going to use the default notification center for our application and the method postNotificationName:object: to send the notification. The parameters are a string for the name of the notification and the object sending he notification.

#pragma mark -
#pragma mark Notification methods
 
- (void)sendNumberPadPressedNotification {
     [[NSNotificationCenter defaultCenter] postNotificationName:@"number pad pressed" 
                                                         object:self];
}

Next we only need to call this function every time we want to notify of the change or event, for example if a button is pressed.

#pragma mark -
#pragma mark Notification methods
- (IBAction)ceroButtonPushed:(id)sender {
     [input setString:[numberFormater stringFromNumber:[NSNumber numberWithFloat:0.00]]];
     coma = NO;
     decimal = 1;
     [self sendNumberPadPressedNotification];
}

Notice we call our method in the last line, in this example the notification is used to let know of the changes in input.

This is all we need to do for object A (the one sending the notification). Lets se now the code for the object B (the one looking for the changes).

First we have to register B in the notification center to be able to get them, we need to use the same notification center as in A, in this case the default one.

We use the method addObserver:selector:name:object:, the parameters are: the object to be registered, a selector to the method to call after getting the notification, the name of the notification we register for and the object we want to be notified for, this last one can be nil or the same object sending the notification, in case we have more than one object sending the same notification and we want to know who sent it.

 // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
 - (void)viewDidLoad {
     [super viewDidLoad];
     numberPadController = [[GenericNumberPad alloc] initWithNibName:@"GenericNumberPad"
     	  						      bundle:[NSBundle mainBundle]
							       frame:CGRectMake(20, 190, 280, 216)];
     [[NSNotificationCenter defaultCenter] addObserver:self 
                                              selector:@selector(numberPadPressed)
						  name:@"number pad pressed" object:nil];
     [[self view] addSubview:[numberPadController view]];
 }

As you can see in the code, the name of the notification is the same as the one we use in object A. Now we only need to do some stuff in the function we gave.

#pragma mark -
#pragma mark Notification methods
 
- (void)numberPadPressed {
     [importLabel setText:[numberPadController input]];
}

In this example we use the notifications to refresh a label with the value of the variable input each time it’s value changed.

VN:F [1.9.8_1114]
Rating: 5.0/5 (1 vote cast)
VN:F [1.9.8_1114]
Rating: 0 (from 0 votes)
Tracking changes or events with notifications, 5.0 out of 5 based on 1 rating

Leave a Reply

You must be logged in to post a comment.

preload preload preload