码迷,mamicode.com
首页 > 移动开发 > 详细

IOS - KVO

时间:2016-05-13 14:07:21      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:

#import <Foundation/Foundation.h>

@interface MYClass : NSObject
@property(assign,nonatomic)int number;
@end


-----------------------

#import "ViewController.h"
#import "MYClass.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIButton *plusButton;
@property (weak, nonatomic) IBOutlet UILabel *showLabel;
@property(nonatomic,strong)MYClass *myClass;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
     _myClass = [[MYClass alloc] init];
    
    [_myClass addObserver:self forKeyPath:@"number" options:NSKeyValueObservingOptionNew context:nil];
    
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{
    if ([keyPath isEqualToString:@"number"]&&object == _myClass) {
        _showLabel.text = [NSString stringWithFormat:@"%@",[change valueForKey:@"new"]];
    }
}

- (IBAction)makeCountPlus:(id)sender {
    
    ++_myClass.number;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
  
    [self removeObserver:self forKeyPath:@"number"];
}

@end

 转自啊左

IOS - KVO

标签:

原文地址:http://www.cnblogs.com/garywong1949/p/5487596.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!