SlideShare a Scribd company logo
1 of 16
Download to read offline
Chapter 5
                Bit Academy
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:)
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.
Class Method
               + (id)defaultCenter
               Return Value
               The current process’s default notification center,
               which is used for system notifications.
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.
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.
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.
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.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(uiUpdate:) name:@"CandleDidChanged" object:nil];




                                                  LightTheCandleAppDelegate

default center                                                                   observer
                 Observer            : LightTheCandleAppDelegate



      LightTheCandleAppDelegate.m                       applicationDidFinishLaunching
setCandleState   postNotification




                            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        notification
                                              .
                                          candleState                    uiUpdate                notification

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




                 candle                          NSNotification                         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

Similar to 아이폰강의(3)

Observer design pattern
Observer design patternObserver design pattern
Observer design patternSara Torkey
 
Distributing information on iOS
Distributing information on iOSDistributing information on iOS
Distributing information on iOSMake School
 
What's New in User Notifications Framework - WWDC16. Meetup @Wantedly with 日本...
What's New in User Notifications Framework - WWDC16. Meetup @Wantedly with 日本...What's New in User Notifications Framework - WWDC16. Meetup @Wantedly with 日本...
What's New in User Notifications Framework - WWDC16. Meetup @Wantedly with 日本...将之 小野
 
iOS NotificationCenter intro
iOS NotificationCenter introiOS NotificationCenter intro
iOS NotificationCenter introJintin Lin
 
From C++ to Objective-C
From C++ to Objective-CFrom C++ to Objective-C
From C++ to Objective-Ccorehard_by
 
Design patterns
Design patternsDesign patterns
Design patternsISsoft
 
iOS 101 - Xcode, Objective-C, iOS APIs
iOS 101 - Xcode, Objective-C, iOS APIsiOS 101 - Xcode, Objective-C, iOS APIs
iOS 101 - Xcode, Objective-C, iOS APIsSubhransu Behera
 

Similar to 아이폰강의(3) (8)

Observer design pattern
Observer design patternObserver design pattern
Observer design pattern
 
Distributing information on iOS
Distributing information on iOSDistributing information on iOS
Distributing information on iOS
 
Notifications
NotificationsNotifications
Notifications
 
What's New in User Notifications Framework - WWDC16. Meetup @Wantedly with 日本...
What's New in User Notifications Framework - WWDC16. Meetup @Wantedly with 日本...What's New in User Notifications Framework - WWDC16. Meetup @Wantedly with 日本...
What's New in User Notifications Framework - WWDC16. Meetup @Wantedly with 日本...
 
iOS NotificationCenter intro
iOS NotificationCenter introiOS NotificationCenter intro
iOS NotificationCenter intro
 
From C++ to Objective-C
From C++ to Objective-CFrom C++ to Objective-C
From C++ to Objective-C
 
Design patterns
Design patternsDesign patterns
Design patterns
 
iOS 101 - Xcode, Objective-C, iOS APIs
iOS 101 - Xcode, Objective-C, iOS APIsiOS 101 - Xcode, Objective-C, iOS APIs
iOS 101 - Xcode, Objective-C, iOS APIs
 

Recently uploaded

Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Victor Rentea
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxRemote DBA Services
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Orbitshub
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Angeliki Cooney
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityWSO2
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusZilliz
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Bhuvaneswari Subramani
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 

Recently uploaded (20)

Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 

아이폰강의(3)

  • 1. Chapter 5 Bit Academy
  • 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.
  • 9. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(uiUpdate:) name:@"CandleDidChanged" object:nil]; LightTheCandleAppDelegate default center observer Observer : LightTheCandleAppDelegate LightTheCandleAppDelegate.m applicationDidFinishLaunching
  • 10. setCandleState postNotification 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 notification . candleState uiUpdate notification setCandleState newState ! myCandle candleState ! ! ! candleState newState; ! ! candleImageView setImage myCandle.candleOnImage ; ! defaultCenter ! ! postNotificationName object ; ! ! ! ! candleImageView setImage myCandle.candleOffImage ; ! candle NSNotification 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 ; ! ! !