I have the following code aiming to catch the event of a NSUserDefaults value changing for a particular key.
[[NSUserDefaults standardUserDefaults] addObse
Edit: viewDidUnload
is now deprecated, use dealloc
instead to removeObserver
This should work perfectly, I have just tested here.
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSUserDefaults standardUserDefaults] addObserver:self
forKeyPath:@"SomeKey"
options:NSKeyValueObservingOptionNew
context:NULL];
// Testing...
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:@"test" forKey:@"SomeKey"];
[defaults synchronize];
}
- (void)viewDidUnload
{
[super viewDidUnload];
[[NSUserDefaults standardUserDefaults] removeObserver:self forKeyPath:@"SomeKey"];
}
- (void)observeValueForKeyPath:(NSString *) keyPath ofObject:(id) object change:(NSDictionary *) change context:(void *) context
{
if([keyPath isEqual:@"SomeKey"])
{
NSLog(@"SomeKey change: %@", change);
}
}
Things you could test.
Put a break point in viewDidUnload and make sure the view isn't disappearing on you (since you are changing SomeKey
from another viewController) If this is the case then maybe move your register/de-register code into init/dealloc depending on how your VC works.
Use explicit KeyPath like @"SomeKey"
not a substitution like SOME_NSSTRING_VARIABLE