I am using NSUserDefaults in my application, and I would like to be notified when a particular value is changed. To do this, I added the following lines to viewDidLoad:
NSUserDefaults *settings = [NSUserDefaults standardUserDefaults]; [settings synchronize]; [settings addObserver:self forKeyPath:@"pref_server" options:NSKeyValueObservingOptionNew context:NULL];
And the method for notification:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { NSLog(@"Change"); NSUserDefaults *settings = [NSUserDefaults standardUserDefaults]; if (object == settings && [keyPath isEqualToString:@"pref_server"]) { NSLog(@"Server did change"); } }
Unfortunately, the latter is never called ... @ "pref_server" is the identifier of the element that I set in Root.plist, in Settings.bundle. What am I doing wrong?
ios
J0o0 Apr 19 '11 at 9:18 2011-04-19 09:18
source share