我有大约四个类让我们假设 A、B、C 和 D,现在我的类 B、C、D 需要观察类 A 的值并在值更改时得到通知。我已经观察到 B 类中的值,但我无法在其他两个类中得到通知,例如 C 和 D。在此先感谢..
- (void)viewDidLoad
{
[super viewDidLoad];
newClassAToBeObserved=[[ClassATobeObserved alloc]init];
[newClassAToBeObserved addObserver:self forKeyPath:@"StatusToken" options:NSKeyValueObservingOptionNew context:NULL];
ClassB*classB=[[ClassB alloc]init];
[classB func];
ClassC*classc=[[ClassC alloc]init];
[classc func];
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ([keyPath isEqualToString:@"StatusToken"])
{
NSLog(@"changed value is : %@",[object valueForKeyPath:keyPath]);
}
}
-(IBAction)ClickIt
{
[newClassAToBeObserved setStatusToken:@"TokenExpired"];
NSLog(@"Value-->%@",newClassAToBeObserved.StatusToken);
}