The KVO mechanism of IOS __ios

Source: Internet
Author: User
Tags ticket
the KVO mechanism of IOS Overview

Key-value Observing (abbreviated KVO): Allows the object to receive notification mechanisms when the properties of the specified object have been modified. Each time a specified object's properties are modified, KVO automatically notifies the corresponding observer. KVO Advantages

When a property changes, KVO provides an automatic message notification. There are many benefits to this architecture. First, developers do not need to implement this scenario themselves: send a message notification every time a property changes. This is the biggest advantage that the KVO mechanism offers. Because this program has been clearly defined, access to framework-level support can be easily adopted. Developers do not need to add any code, they do not need to design their own observer model, can be used directly in the project. Second, the KVO architecture is very powerful, and it is easy to support multiple observers to observe the same attribute and related values. instance

Suppose a scene, the price of a movie ticket is displayed on the current screen, when the movie ticket price changes, real-time display update its price 1, definition Datamodel

#import <Foundation/Foundation.h>
@interface movieticket:nsobject
@property (nonatomic, Strong) NSString *title;
@property (nonatomic, assign) float price;
@end

#import "MovieTicket.h"
@implementation movieticket
@end
2, Viewcontrol interface

3, Viewcontrol code

#import "ViewController.h" #import "MovieTicket.h" nsstring *const kchange = @ "Change";
@interface Viewcontroller () @property (weak, nonatomic) Iboutlet Uilabel *titlelab;

@property (Weak, nonatomic) Iboutlet Uilabel *pricelab;

@property (nonatomic, strong) Movieticket *ticket;

    @end @implementation Viewcontroller-(void) viewdidload {[Super viewdidload];
    Movieticket *ticket = [[Movieticket alloc] init];
    Ticket.title = @ "Iron Man";

    Ticket.price = 1.99;
    Self.titleLab.text = Ticket.title;

    Self.priceLab.text = [NSString stringwithformat:@ "%f", Ticket.price]; 1, register, set the attributes of the observed, the observer is ticket, the observer is the current controller//Note: The object is established between the connection, rather than the two classes [ticket addobserver:self forkeypath:kchange opt

    ions:0 Context:null]; Observe the price attribute of the ticket individually without implementing Willchangevalueforkey: And Didchangevalueforkey: Method//[ticket addobserver:self FORKEYP

    ath:@ "Price" options:0 context:null];
Self.ticket = ticket; }-(void) Dealloc {//3, removal observation [Self.ticket removeobserver:Self Forkeypath:kchange]; //Click "Change Fares" analog change Properties-(ibaction) Changepricebtnclick: (ID) Sender {//write the code that changes the attribute in Willchangevalueforkey: and Didchangev
    Alueforkey: Between [Self.ticket Willchangevalueforkey:kchange];
    Self.ticket.title = @ "Captain America";
    Self.ticket.price = Arc4random ();
[Self.ticket Didchangevalueforkey:kchange]; }//2, implement the callback method in the Observer, call-(void) Observevalueforkeypath: (NSString *) KeyPath Ofobject: (ID) o when the monitored properties of the observer are changed

    Bject change: (nsdictionary<nsstring *,id> *) Change context: (void *) Context {
    if ([KeyPath Isequaltostring:kchange]) {self.titleLab.text = Self.ticket.title;
    Self.priceLab.text = [NSString stringwithformat:@ "%f", Self.ticket.price];
 } if ([KeyPath isequaltostring:@ "Price"]) {}} @end
reference materials

Http://www.cnblogs.com/pengyingh/articles/2383629.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.