Changes to an attribute of the iOS monitoring control: observeValueForKeyPath, valueforkeypath

Source: Internet
Author: User

Changes to an attribute of the iOS monitoring control: observeValueForKeyPath, valueforkeypath

 

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 (30, 50, 50, 30)]; [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 status of the listener button [self. button addObserver: self forKeyPath: @ "enabled" options: encontext: @ "test_button"]; // change the enabled status 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 ;});}

 

Add listener observer

/*** Method for changing the listener button status ** @ param keyPath ** @ param object ** @ param change changed data * @ param context when the listener is registered passed 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. the enabled attribute of the button has changed % @ ", [change objectForKey: @" new "]);}

 

Log output

11:48:32. 001 listening for a widget attribute change [34212: 570638] The enabled attribute of self. button has changed 1

 

If you did not see this article in wb145230, click to view the original article.

 

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.