Search code examples
iphoneobjective-cobserver-patterniphone-sdk-3.1.3nsnotificationcenter

Adding an observer to an NSString


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?


Solution

  • 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;