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)