Use below code it will call when your BOOL
value changed.You can use KVO to observe the value of your myKey
.Write this in any class where you observe the values.Write it in viewDidLoad
in vc2
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults addObserver:self
forKeyPath:@"myKey"
options:NSKeyValueObservingOptionNew
context:NULL];
Implement this method in your class where you have added observer in vc2
-(void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context
{
//check here if it is myKey than do something
if([keyPath isEqualToString:@"myKey"]){
//do your work
NSLog(@"KVO: %@ changed property %@ to value %@", object, keyPath, change);
BOOL newValue = [[change objectForKey: NSKeyValueChangeNewKey] boolValue];
}
else{
NSLog(@"Not my key");
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
Remove observer in vc2
dealloc
-(void)dealloc{
[[NSUserDefaults standardUserDefaults] removeObserver:self forKeyPath:@"myKey"];
}
In observeValueForKeyPath
check for if value changed for myKey
than do whatever you want to.