01.02.2014 Views

Objective-C Fundamentals

Objective-C Fundamentals

Objective-C Fundamentals

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

198 CHAPTER 9 Memory management<br />

new notifications and observers to register their interest in receiving particular types<br />

of notifications. A notification center is similar in concept to an email distribution list.<br />

One or more email addresses (observers) are registered with the mailing list (notification<br />

center), and when a new email (notification) is sent by a sender, the mailing list<br />

distributes it to all currently registered email addresses. A given email address may not<br />

receive all emails sent to the server because it’s unlikely the address will be registered<br />

with every email list offered by the mail server.<br />

In <strong>Objective</strong>-C the first step in handling a notification is to define a method you<br />

want invoked whenever a suitable notification occurs. For example, you could define<br />

the following method in the CTRentalProperty class:<br />

- (void)HandleLowMemoryWarning:(NSNotification *)notification<br />

{<br />

NSLog(@"HandleLowMemoryWarning was called");<br />

}<br />

Once the method is defined, you must inform the NSNotificationCenter that you’re<br />

interested in observing a particular type of notification. This can be done by sending<br />

an addObserver:selector:name:object: message, as shown in the following listing.<br />

Listing 9.3<br />

Registering intent to observe memory warning notifications<br />

- (id)initWithAddress:(NSString *)newAddress<br />

rentalPrice:(float)newRentalPrice<br />

andType:(PropertyType)newPropertyType {<br />

if ((self = [super init])) {<br />

self.address = newAddress;<br />

self.rentalPrice = newRentalPrice;<br />

self.propertyType = newPropertyType;<br />

}<br />

[[NSNotificationCenter defaultCenter]<br />

addObserver:self<br />

selector:@selector(HandleLowMemoryWarning)<br />

name:UIApplicationDidReceiveMemoryWarningNotification<br />

object:nil];<br />

}<br />

return self;<br />

The observer and selector arguments specify which object should act as the observer<br />

and the message that object wishes to be sent when a suitable notification occurs. The<br />

last two arguments specify the particular notification you’re interested in observing.<br />

This example specifies that you want to observe a notification called UIApplication-<br />

DidReceiveMemoryWarningNotification. Passing nil for the object argument means<br />

that you’re interested in observing this event no matter who the sender is. If you’re<br />

interested only in notifications generated by a particular object, you can specify that<br />

object instead to filter out notifications generated by other objects.<br />

Registering with a notification center indicates your intent to observe a notification<br />

event, so it’s natural to need to unregister your intent when you no longer want to

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!