//观察者事件 (!!注册必删除)
[[IndexViewInformation sharedInstance] addObserver:self forKeyPath:@"selectedMoreModel" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
[[IndexViewInformation sharedInstance] addObserver:self forKeyPath:@"selectedBuyModel" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
//删除注册的观察者事件
[[IndexViewInformation sharedInstance] removeObserver:self forKeyPath:@"selectedBuyModel"];
[[IndexViewInformation sharedInstance] removeObserver:self forKeyPath:@"selectedMoreModel"];
//观察者触发的事件,在观察者观察到变化的时候触发
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([keyPath isEqualToString:@"selectedMoreModel"]) {
UIStoryboard *board=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
ProjectDetailViewController *pro =[board instantiateViewControllerWithIdentifier:@"ProjectDetail"];
pro.shengyuTime = [IndexViewInformation sharedInstance].selectedMoreModel.thenDays;
pro.projectID = [IndexViewInformation sharedInstance].selectedMoreModel.prodectId;
pro.thenDays = pro.shengyuTime;
[self.navigationController pushViewController:pro animated:YES];
NSLog(@"触发MoreModel观察者~!");
}
if ([keyPath isEqualToString:@"selectedBuyModel"]) {
NSLog(@"触发BuyModel观察者~!");
[self requestBeforeBuy];
}
}
再次强调,观察者添加必有删除,如果忘记删除,会导致意想不到的麻烦。
本文有因为问题请联系
QQ:563699115
Telephone:18341266547