且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

检测NSUserDefaults上的更改

更新时间:2023-02-26 14:06:18

使用以下代码片段试用 NSUserDefaultsDidChangeNotification

try out the NSUserDefaultsDidChangeNotification with this code snippet:

- (id)init {

  self = [super init];

  if(self) {
     [[NSNotificationCenter defaultCenter] addObserver:self
                                              selector:@selector(defaultsChanged:)
                                                  name:NSUserDefaultsDidChangeNotification
                                                object:nil];
  }
  return self;    
}

- (void)defaultsChanged:(NSNotification *)notification {
  // Get the user defaults
  NSUserDefaults *defaults = (NSUserDefaults *)[notification object];

  NSLog(@"%@", [defaults objectForKey:@"yourIntrestedObject"]);
}

- (void)dealloc {
 [[NSNotificationCenter defaultCenter] removeObserver:self];
}