Using KVO to monitor the change of the control's properties (Observevalueforkeypath)

Source: Internet
Author: User

To create a test UIButton

#import "ViewController.h" @interface Viewcontroller () @property (nonatomic, strong) UIButton *button;@ End@implementation viewcontroller-(void) viewdidload {    [super viewdidload];        Self.button = [[UIButton alloc] Initwithframe:cgrectmake ((a);    [Self.button settitle:@ "test" forstate:uicontrolstatenormal];    [Self.button Settitlecolor:[uicolor Blackcolor] forstate:uicontrolstatenormal];    Self.button.layer.borderWidth = 1.0f;    [Self.view AddSubview:self.button];
Register the enabled state of the monitor button    [Self.button addobserver:self forkeypath:@ "enabled" options: Nskeyvalueobservingoptionnew context:@ "Test_button"];  Change the enabled state of the current button after 3 seconds    Dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (3.0f * nsec_per_sec )), Dispatch_get_main_queue (), ^{        self.button.enabled = YES;    });

Adding listener observers

/** *  How to change the status of the monitor button * *  @param keypath button Changed properties *  @param object  button *  @param  Change the data after changes *  @ param Context Register monitoring when the context passes over the value */
-(void) Observevalueforkeypath: (NSString *) KeyPath Ofobject: (ID) object change: (nsdictionary *) Change context: (void *) Context {    UIButton *button = (UIButton *) object;    if (Self.button = = Button && [@ "Enabled" Isequaltostring:keypath]) {        NSLog (@ "Self.button's Enabled property changed%@", [Change objectforkey:@ "new"]);}    }

Using KVO to monitor the change of the control's properties (Observevalueforkeypath)

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.