如何接收NSUserDefaultsDidChangeNotification iphone [英] How to receive NSUserDefaultsDidChangeNotification iphone
问题描述
经过大量搜索,我无法找到你需要传递字典对象的东西:
After a lot of searching I was not able to find wheather you need to pass a dictionary object to:
[NSUserDefaultsDidChangeNotification addObserver: forKeyPath: options: context:];
如果我想在userDefaults中进行一次更改通知我应该在选项中提供什么。还有什么是keypath?
and what should be provided in options if I want to be notified for even a single change in the userDefaults. Also what is keypath?
提前完成。
推荐答案
NSUserDefaultsDidChangeNotification
只是在更改默认值时发出的通知。要听取它,你需要这个代码:
NSUserDefaultsDidChangeNotification
is just a notification that is sent out when the defaults are changed. To listen out for it you need this code :
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self
selector:@selector(defaultsChanged:)
name:NSUserDefaultsDidChangeNotification
object:nil];
这将调用方法defaultsChanged:触发通知时。你需要像这样实现这个方法:
This will call the method defaultsChanged: when the notification is fired. You need to implement this method like this :
- (void)defaultsChanged:(NSNotification *)notification {
// Get the user defaults
NSUserDefaults *defaults = (NSUserDefaults *)[notification object];
// Do something with it
NSLog(@"%@", [defaults objectForKey:@"nameOfThingIAmInterestedIn"]);
}
这篇关于如何接收NSUserDefaultsDidChangeNotification iphone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!