首页 > 移动平台 > 详细

IOS - KVO

时间:2016-05-13 14:05:17      阅读:215      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!