2. NSNotificationCenter &
NSNotification
CandleDidChanged addObserver: self
uiUpdate name:@”CandleDidChanged”
object B
CandleDidChanged
perform:@selector(uiUpdate:)
post:@”CandleDidChanged”
Notification object C
object A Center
addObserver: self
name:@”CandleDidChanged”
CandleDidChanged object D
candleUpdate
perform:@selector(candleUpdate:)
3. NSNotificationCenter Class
Inherits from
NSObject
Conforms to
NSObject (NSObject)
Framework
/System/Library/Frameworks/
Foundation.framework
Availability
Available in iOS 2.0 and later.
Companion guide
Notification Programming Topics
Declared in
NSNotification.h
A notification center maintains a notification dispatch table which
specifies a notification set for a particular observer. A notification set is a
subset of the notifications posted to the notification center. Each table entry
contains three items:
• Notification observer: Required. The object to be notified when
qualifying notifications are posted to the notification center.
• Notification name: Optional. Specifying a name reduces the set of
notifications the entry specifies to those that have this name.
• Notification sender: Optional. Specifying a sender reduces the set of
notifications the entry specifies to those sent by this object.
4. Class Method
+ (id)defaultCenter
Return Value
The current process’s default notification center,
which is used for system notifications.
5. Instance Method
- (void)addObserver:(id)notificationObserver selector:(SEL)
notificationSelector name:(NSString *)notificationName object:(id) - (void)postNotificationName:(NSString
notificationSender *)notificationName object:(id)
Parameters notificationSender userInfo:(NSDictionary
notificationObserver *)userInfo
Object registering as an observer. This value must not be nil.
notificationSelector Creates a notification with a given name, sender,
Selector that specifies the message the receiver sends and information and posts it to the receiver.
notificationObserver to notify it of the notification posting. The method
specified by notificationSelector must have one and only one argument Parameters
(an instance of NSNotification). notificationName
The name of the notification.
notificationName
The name of the notification for which to register the observer; that is, notificationSender
only notifications with this name are delivered to the observer. The object posting the notification.
userInfo
If you pass nil, the notification center doesn’t use a notification’s name
Information about the the notification. May be
to decide whether to deliver it to the observer. nil.
notificationSender
The object whose notifications the observer wants to receive; that is, only
notifications sent by this sender are delivered to the observer.
If you pass nil, the notification center doesn’t use a notification’s sender
to decide whether to deliver it to the observer.
- (void)postNotification:(NSNotification *)notification
Posts a given notification to the receiver.
Parameters
notification
The notification to post. This value must not be nil.
Discussion
You can create a notification with the NSNotification class method
notificationWithName:object: or notificationWithName:object:userInfo:. An
exception is raised if notification is nil.
6. Notification Class
Inherits from
NSObject Notification Class : NotificationCeneter
Conforms to
NSCoding
NSCopying
NSObject (NSObject)
Framework
/System/Library/Frameworks/Foundation.framework
Availability
Available in iOS 2.0 and later.
7. NSNotification Class Method
+ (id)notificationWithName:(NSString *)aName object:(id)anObject
Returns a new notification object with a specified name and object.
Parameters
aName : The name for the new notification. May not be nil.
anObject : The object for the new notification.
+ (id)notificationWithName:(NSString *)aName object:(id)anObject
userInfo:(NSDictionary *)userInfo
Returns a notification object with a specified name, object, and user information.
Parameters
aName : The name for the new notification. May not be nil.
anObject : The object for the new notification.
userInfo : The user information dictionary for the new
notification. May be nil.
8. NSNotification Instance Method
- (NSString *)name
The name of the notification. Typically you use this method to find out what kind of
notification you are dealing with when you receive a notification.
- (id)object(sender object)
The object associated with the notification. This is often the object that posted this
notification. It may be nil.
Typically you use this method to find out what object a notification applies to when
you receive a notification.
- (NSDictionary *)userInfo
Returns the user information dictionary associated with the receiver. May be nil.
The user information dictionary stores any additional objects that objects receiving
the notification might
use.
12. Key Value Coding
- applications to access the properties of an object
indirectly by name (or key), rather than directly through
invocation of an accessor method or as instance
variables.
- Key-value coding is a key technology when working
with key-value observing
- NSObject NSObject
- NSDictionary
BOOL candleStateValue = [myCandle candleState];
[myCandle setCandleState:!candleStateValue];
BOOL candleStateValue = myCandle.candleState; property
myCandle.candleState = !candleStateValue;
BOOL candleStateValue = [myCandle valueForKey: @”candleState”]; KVC
[myCandle setValue : !candleStateValue forKey: @”candleState”];
KVC Candle getter & setter