@interface ZXViewController ()
@property (nonatomic, strong) NSProgress *pro;
- (IBAction)btnOnClick:(id)sender;
@end
@implementation ZXViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSProgress *pro = [NSProgress progressWithTotalUnitCount:10000.0];
// 注册一个监听器 KVO
[pro addObserver:self forKeyPath:@"fractionCompleted" options:NSKeyValueObservingOptionNew context:nil];
}
- (IBAction)btnOnClick:(id)sender {
// 模拟改变他的属性
self.pro.completedUnitCount += 100;
}
// 监听到了属性改变会调用这个方法
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
NSLog(@"lskdjfl= %f",self.pro.fractionCompleted);
}
@end
时间: 2024-10-13 14:09:39