NSNotiﬁcationCenter & NSNotiﬁcation CandleDidChanged addObserver: self uiUpdate name:@”CandleDidChanged” object BCandleDidChanged perform:@selector(uiUpdate:) post:@”CandleDidChanged” Notiﬁcation object C object A Center addObserver: self name:@”CandleDidChanged” CandleDidChanged object D candleUpdate perform:@selector(candleUpdate:)
NSNotiﬁcationCenter Class Inherits from NSObject Conforms to NSObject (NSObject) Framework /System/Library/Frameworks/ Foundation.framework Availability Available in iOS 2.0 and later. Companion guide Notiﬁcation Programming Topics Declared in NSNotiﬁcation.h A notiﬁcation center maintains a notiﬁcation dispatch table which speciﬁes a notiﬁcation set for a particular observer. A notiﬁcation set is a subset of the notiﬁcations posted to the notiﬁcation center. Each table entry contains three items: • Notiﬁcation observer: Required. The object to be notiﬁed when qualifying notiﬁcations are posted to the notiﬁcation center. • Notiﬁcation name: Optional. Specifying a name reduces the set of notiﬁcations the entry speciﬁes to those that have this name. • Notiﬁcation sender: Optional. Specifying a sender reduces the set of notiﬁcations the entry speciﬁes to those sent by this object.
Class Method + (id)defaultCenter Return Value The current process’s default notiﬁcation center, which is used for system notiﬁcations.
Instance Method - (void)addObserver:(id)notiﬁcationObserver selector:(SEL) notiﬁcationSelector name:(NSString *)notiﬁcationName object:(id) - (void)postNotificationName:(NSString notiﬁcationSender *)notiﬁcationName object:(id) Parameters notiﬁcationSender userInfo:(NSDictionary notiﬁcationObserver *)userInfo Object registering as an observer. This value must not be nil. notiﬁcationSelector Creates a notiﬁcation with a given name, sender, Selector that speciﬁes the message the receiver sends and information and posts it to the receiver. notiﬁcationObserver to notify it of the notiﬁcation posting. The method speciﬁed by notiﬁcationSelector must have one and only one argument Parameters (an instance of NSNotification). notiﬁcationName The name of the notiﬁcation. notiﬁcationName The name of the notiﬁcation for which to register the observer; that is, notiﬁcationSender only notiﬁcations with this name are delivered to the observer. The object posting the notiﬁcation. userInfo If you pass nil, the notiﬁcation center doesn’t use a notiﬁcation’s name Information about the the notiﬁcation. May be to decide whether to deliver it to the observer. nil. notiﬁcationSender The object whose notiﬁcations the observer wants to receive; that is, only notiﬁcations sent by this sender are delivered to the observer. If you pass nil, the notiﬁcation center doesn’t use a notiﬁcation’s sender to decide whether to deliver it to the observer. - (void)postNotification:(NSNotification *)notiﬁcation Posts a given notiﬁcation to the receiver. Parameters notiﬁcation The notiﬁcation to post. This value must not be nil. Discussion You can create a notiﬁcation with the NSNotification class method notificationWithName:object: or notificationWithName:object:userInfo:. An exception is raised if notiﬁcation is nil.
Notiﬁcation Class Inherits from NSObject Notiﬁcation Class : NotiﬁcationCeneter Conforms to NSCoding NSCopying NSObject (NSObject) Framework /System/Library/Frameworks/Foundation.framework Availability Available in iOS 2.0 and later.
NSNotiﬁcation Class Method+ (id)notificationWithName:(NSString *)aName object:(id)anObjectReturns a new notiﬁcation object with a speciﬁed name and object.ParametersaName : The name for the new notification. May not be nil.anObject : The object for the new notification.+ (id)notificationWithName:(NSString *)aName object:(id)anObjectuserInfo:(NSDictionary *)userInfoReturns a notiﬁcation object with a speciﬁed name, object, and user information.ParametersaName : The name for the new notiﬁcation. May not be nil.anObject : The object for the new notification.userInfo : The user information dictionary for the newnotification. May be nil.
NSNotiﬁcation Instance Method - (NSString *)name The name of the notiﬁcation. Typically you use this method to ﬁnd out what kind of notiﬁcation you are dealing with when you receive a notiﬁcation. - (id)object(sender object) The object associated with the notiﬁcation. This is often the object that posted this notiﬁcation. It may be nil. Typically you use this method to ﬁnd out what object a notiﬁcation applies to when you receive a notiﬁcation. - (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 notiﬁcation might use.
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