The Observer mode for iOS learning

Source: Internet
Author: User

Viewer mode:

There are two specific applications for observers: notification mechanism (notification) and KVO (key-value-observing) mechanism

Notification mechanism:

Who wants to monitor the value of the change, who will register notice, in particular, notice that the recipient of the notification must exist this prerequisite without notice, remember to remove.

    1. In the third interface, build a notification hub, send notifications via notification hubs (the process of sending notifications is the process of passing values, passing the value of the transfer as the value of the object to the first interface
    2. In the first interface, a notification center, through the notification, register a listener event
    3. In the first sector, set up the event that receives the notification.
    4. In the dealloc of the first sector, remove the notification centre

    1. Any object pair can send notifications to the center, while any object can listen to the notification of the center.
      The code to send the notification is as follows: [[Nsnotificationcenter Defaultcenter] postnotificationname:@ "Mynotificationname" object: BROADCASTEROBJEC];
    2. The code to register to receive notifications is as follows:

[[Nsnotificationcenter Defaultcenter]

Addobserver:listenerobject selector: @selector (receivingmethodonlistener:) name:@ "Mynotificationname" object:nil];

Implementation: The value of the B interface is passed to the a interface, so the B interface sends notifications to the Notification center, a interface registers as a listener, listens for changes in B values, and accepts the value of B returned from the Notification center. If the C page also wants to accept the value of B, it is also registered as a listener. (So many-to-many can be achieved)

A interface: Rootviewcontroller B Interface: Secondviewcontroller

in the B interface to establish notification center, the value to be passed Nametextfield.text send via notification hubs

// secondviewcontroller.m-(ibaction) NotificationMethod: (ID) sender {[[Nsnotificationcenter Defaultcenter] postnotificationname:@'changenamenotification'  Object : Self  userinfo:@{@'name': self.nametextfield.text}];[ Self Dismissviewcontrolleranimated:yes Completion:nil];}

in the A in the controller of the page, register the notification:

// rootviewcontroller.m-(void) viewdidload.{ [Super Viewdidload]; [[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (changenamenotification:) name:@  'changenamenotification'object: nil];}

Called, displays

// rootviewcontroller.m-(void) Changenamenotification: (nsnotification**namedictionary =  = [namedictionary objectforkey:@'name'];.} When we do not use, remember to delete the notice://rootviewcontroller.m-(void

KVO mechanism:

When the properties of the specified object are modified, the object receives a notification. Simply put, the KVO will automatically notify the appropriate observer when the property of the observed object has been modified each time it is specified.

KVO provides a way to observe a change in a property, greatly simplifying the code. This observation-the observed model is suitable for situations where, for example, a change in a property value of a (data Class) makes a corresponding change in a property in B (view Class).

(Such a mechanism sounds similar to notification, but notification requires an object that sends notification, typically notificationcenter, to inform the observer.) The KVO is a direct notification to the object of observation. )

1 . Implementation Change method: (rewrite)

-(void) Observevalueforkeypath: (NSString *) KeyPath Ofobject: (ID) object change: (nsdictionary *) Change context: (void*) Context

2 . Register to receive changes:

-(void) Addobserver: (NSObject *) anobserver Forkeypath: (NSString *) keypath options: (nskeyvalueobservingoptions) Options context: (void*) context

(KeyPath is the attribute value to observe, options give you the option of observing the change in the key value, and the context facilitates the transfer of the data you need)

Realize:

@property (nonatomic,copy) nsstring * title; @property (nonatomic,assign) Nsuinteger age;

In control where we initialize it and assign a value

   Self.hero = [[Hero Alloc] init];     @" Undead " ;     @" General " ;     102;

Now that we have a basic value for this object, we will have this object's name listen to his changes.

[Self.hero addobserver:self Forkeypath:@ 'name' options:nskeyvalueobservingoptionnew| Nskeyvalueobservingoptionold Context:nil];

Trigger a notification and change the value

-(void) Touchesbegan: (Nsset *) touches withevent: (uievent *)event{    @ " sacrificial ";}

in the development of Callback Letter number, process the notification of changes received

- (void) Observevalueforkeypath: (NSString *) KeyPath Ofobject: (ID)ObjectChange: (nsdictionary *) Change context: (void*) context{if([KeyPath isequaltostring:@"name"]) {NSLog (@"--%@ after assignment", Self.hero.name); NSLog (@"the new value--%@", change[@"New"]); NSLog (@"Previous Value--%@", change[@" Old"]); }}//Last logout Observer- (void) dealloc{[Self.hero removeobserver:self Forkeypath:@"name"];}

The Observer mode for iOS learning

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.