问题
In my application delegate I have an NSString which can be modified by a class. What I want to do is to have another class observe this NSString and to react whenever this NSString is changed.
Does anybody know how I can do this?
回答1:
On the "other class" you must do something like:
[MyAppDelegate addObserver:self forKeyPath:@"VARIABLE_NAME" options:NSKeyValueObservingOptionNew context:nil];
and implement
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context;
来源:https://stackoverflow.com/questions/3281969/adding-an-observer-to-an-nsstring