ºÝºÝߣ

ºÝºÝߣShare a Scribd company logo
Chapter 5
                Bit Academy
NSNoti?cationCenter &
                      NSNoti?cation
               CandleDidChanged                                   addObserver: self
                                      uiUpdate               name:@¡±CandleDidChanged¡±

                                                                       object B


CandleDidChanged
                                                                   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)anObject
Returns a new noti?cation object with a speci?ed 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 noti?cation object with a speci?ed name, object, and user information.

Parameters
aName : 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 new
notification. 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.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(uiUpdate:) name:@"CandleDidChanged" object:nil];




                                                  LightTheCandleAppDelegate

default center                                                                   observer
                 Observer            : LightTheCandleAppDelegate



      LightTheCandleAppDelegate.m                       applicationDidFinishLaunching
setCandleState   postNoti?cation




                            IBAction toggleCandle          sender

                       !      UISwitch  mySwitch      UISwitch      sender;
                       !                	 
                       !      myCandle.candleState         mySwitch isOn ;

                       ?
                                    uiUpdate                 notification

                       !            myCandle candleState
                       !      !
                       !      !     candleImageView setImage myCandle.candleOnImage ;
                       !
                       !      !
                       !      !     candleImageView setImage myCandle.candleOffImage ;
                       !
Candle             candleState
      Candle                                              candleState setter        noti?cation
                                              .
                                          candleState                    uiUpdate                notification

                setCandleState       newState                    !        myCandle candleState
                                                                 !   !
      !        candleState   newState;                           !   !       candleImageView setImage myCandle.candleOnImage ;
      !                          defaultCenter                   !
      !         postNotificationName                object   ;   !   !
                                                                 !   !       candleImageView setImage myCandle.candleOffImage ;
                                                                 !




                 candle                          NSNoti?cation                         LightTheCandleAppDelegate
                 object                             center                                         object
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
Key Value Observing
 -
 - NSObject          NSKeyValueObserving      KVO                NSObject


           observer
           [myCandle addObserver:self forKeyPath:@¡±candleState¡±
           options:NSKeyValueObservingOptionNew|NSKeyValueObservingOld context: nil];




- NSKeyValueObservingOptionNew :
- NSKeyValueObservingOptionOld :
- NSKeyValueObservingOptionInitial :                                                .
addObserver:forKeyPath:options:context:
- NSKeyValueObservingOptionPrior :      KVO
                                           .
Observing Method
                                             .
addObserver:                                               .

- (void) observerValueForKeyPath:(NSString *)keyPath ofObject: (id)object change:(NSDictionary*)change context:(void)context
{
    if([keyPath isEqualToString:@¡±candleState¡±])
    {
         // do something
    }
}
LightTheCandleAppDelegate.m
-applicationDidFinishLaunching
KVC
[myCandle setValue: candleOffImage forKey: @candleOffImage¡±];
[myCandle setValue: candleOnImage forKey: @candleOnImage¡±];
[myCandle setValue: [NSNumber numberWithBool:NO] forKey: @candleState¡±];

KVO
[myCandle addObserver:self forKeyPath: @¡±candleState¡± options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld
context : nil];

-dealloc
[myCandle removeObserver: self forKeyPath:@¡±candleState¡±];
IBAction toggleCandle   sender

               !      sender isKindOfClass UISwitch class
               ! !          newState    sender isOn ;
               ! !     myCandle setValue           numberWithBool newState
               forKey              ;
               !

               ?
                      observeValueForKeyPath            keyPath ofObject          object
               change              change context         context

               !      keyPath isEqualToString
               ! !         newState     change
               valueForKey NSKeyValueChangeNewKey boolValue ;
               ! !      newState
               ! ! !
               ! ! !      candleImageView setImage object
               valueForKey                 ;
               ! ! ! onOffSwitch.on            ;
               ! ! ! candleStateLabel.text              	  	   	    ;
               ! !
               ! ! !
               ! ! !      candleImageView setImage object
               valueForKey                  ;
               ! ! ! onOffSwitch.on           ;
               ! ! ! candleStateLabel.text              	  	  	         	    	    ;
               ! !
	    	    	    !

More Related Content

?????(3)

  • 1. Chapter 5 Bit Academy
  • 2. NSNoti?cationCenter & NSNoti?cation CandleDidChanged addObserver: self uiUpdate name:@¡±CandleDidChanged¡± object B CandleDidChanged perform:@selector(uiUpdate:) post:@¡±CandleDidChanged¡± Noti?cation object C object A Center addObserver: self name:@¡±CandleDidChanged¡± CandleDidChanged object D candleUpdate perform:@selector(candleUpdate:)
  • 3. 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.
  • 4. Class Method + (id)defaultCenter Return Value The current process¡¯s default noti?cation center, which is used for system noti?cations.
  • 5. 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.
  • 6. 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.
  • 7. NSNoti?cation Class Method + (id)notificationWithName:(NSString *)aName object:(id)anObject Returns a new noti?cation object with a speci?ed 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 noti?cation object with a speci?ed name, object, and user information. Parameters aName : 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 new notification. May be nil.
  • 8. 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.
  • 9. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(uiUpdate:) name:@"CandleDidChanged" object:nil]; LightTheCandleAppDelegate default center observer Observer : LightTheCandleAppDelegate LightTheCandleAppDelegate.m applicationDidFinishLaunching
  • 10. setCandleState postNoti?cation IBAction toggleCandle sender ! UISwitch mySwitch UISwitch sender; ! ! myCandle.candleState mySwitch isOn ; ? uiUpdate notification ! myCandle candleState ! ! ! ! candleImageView setImage myCandle.candleOnImage ; ! ! ! ! ! candleImageView setImage myCandle.candleOffImage ; !
  • 11. Candle candleState Candle candleState setter noti?cation . candleState uiUpdate notification setCandleState newState ! myCandle candleState ! ! ! candleState newState; ! ! candleImageView setImage myCandle.candleOnImage ; ! defaultCenter ! ! postNotificationName object ; ! ! ! ! candleImageView setImage myCandle.candleOffImage ; ! candle NSNoti?cation LightTheCandleAppDelegate object center object
  • 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
  • 13. Key Value Observing - - NSObject NSKeyValueObserving KVO NSObject observer [myCandle addObserver:self forKeyPath:@¡±candleState¡± options:NSKeyValueObservingOptionNew|NSKeyValueObservingOld context: nil]; - NSKeyValueObservingOptionNew : - NSKeyValueObservingOptionOld : - NSKeyValueObservingOptionInitial : . addObserver:forKeyPath:options:context: - NSKeyValueObservingOptionPrior : KVO .
  • 14. Observing Method . addObserver: . - (void) observerValueForKeyPath:(NSString *)keyPath ofObject: (id)object change:(NSDictionary*)change context:(void)context { if([keyPath isEqualToString:@¡±candleState¡±]) { // do something } }
  • 15. LightTheCandleAppDelegate.m -applicationDidFinishLaunching KVC [myCandle setValue: candleOffImage forKey: @candleOffImage¡±]; [myCandle setValue: candleOnImage forKey: @candleOnImage¡±]; [myCandle setValue: [NSNumber numberWithBool:NO] forKey: @candleState¡±]; KVO [myCandle addObserver:self forKeyPath: @¡±candleState¡± options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context : nil]; -dealloc [myCandle removeObserver: self forKeyPath:@¡±candleState¡±];
  • 16. IBAction toggleCandle sender ! sender isKindOfClass UISwitch class ! ! newState sender isOn ; ! ! myCandle setValue numberWithBool newState forKey ; ! ? observeValueForKeyPath keyPath ofObject object change change context context ! keyPath isEqualToString ! ! newState change valueForKey NSKeyValueChangeNewKey boolValue ; ! ! newState ! ! ! ! ! ! candleImageView setImage object valueForKey ; ! ! ! onOffSwitch.on ; ! ! ! candleStateLabel.text ; ! ! ! ! ! ! ! ! candleImageView setImage object valueForKey ; ! ! ! onOffSwitch.on ; ! ! ! candleStateLabel.text ; ! ! !