iOS NSUserDefaults观察单个键的值更改 [英] iOS NSUserDefaults Watching the change of values for a single key
问题描述
我有以下代码,旨在捕获特定键的NSUserDefaults值更改事件。
I have the following code aiming to catch the event of a NSUserDefaults value changing for a particular key.
[[NSUserDefaults standardUserDefaults] addObserver:self
forKeyPath:SOME_NSSTRING_VARIABLE
options:NSKeyValueObservingOptionNew
context:NULL];
- (void)observeValueForKeyPath:(NSString *) keyPath ofObject:(id) object change:(NSDictionary *) change context:(void *) context
{NSLog (@"Changed for key %@", keyPath); }
但是从不调用observeValueForKeyPath。我甚至尝试用字符串替换SOME_NSSTRING_VARIABLE,如观察值的变化NSUserDefaults键但它没有帮助。
But the observeValueForKeyPath is never getting called. I even tried replacing the SOME_NSSTRING_VARIABLE too with a string as mentioned in Observing value changes to an NSUserDefaults key but it has not helped.
更新:
我正在从tabview更改NSUserDefaults。上面监视更改的代码位于同一tabviewcontroller的不同选项卡中。在我监视更改的选项卡(上面代码所在的选项卡)中,如果我添加:
Update: I am changing the NSUserDefaults from a tabview. The above code to monitor changes is in a different tab of the same tabviewcontroller. In the tab where I monitor for the changes (the tab where above code exists), if I add a :
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// NSLog(@"viewillappear");
NSUserDefaults *FUDefaults = [NSUserDefaults standardUserDefaults];
NSLog(@"The username obtained is: %@", [FUDefaults objectForKey:SOME_NSSTRING_VARIABLE]);
}
正确获取更新的NSUserDefaults值,但从未调用observeValueForKeyPath。 / p>
the updated NSUserDefaults value is obtained correctly, but the observeValueForKeyPath was never called.
推荐答案
编辑: viewDidUnload
现已弃用,请使用 dealloc
改为 removeObserver
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);
}
}
你可以测试的东西。
-
在viewDidUnload中放置一个断点并确保视图没有消失(因为你要更改
SomeKey 来自另一个viewController的code>如果是这种情况,那么可能会根据VC的工作方式将注册/取消注册代码移动到init / dealloc中。
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.
使用显式KeyPath,如 @SomeKey
不是像 SOME_NSSTRING_VARIABLE
Use explicit KeyPath like @"SomeKey"
not a substitution like SOME_NSSTRING_VARIABLE
这篇关于iOS NSUserDefaults观察单个键的值更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!