diff options
| author | Vadim Shpakovski | 2013-04-16 14:57:23 +0300 | 
|---|---|---|
| committer | Vadim Shpakovski | 2013-04-16 14:57:23 +0300 | 
| commit | 278c5fa2a5f50b1281929179a570403b605015e5 (patch) | |
| tree | ac770c966b74a0872fb285706ce00debac065f84 | |
| parent | b3e2e54830769c32c017c707793ddc30adc31798 (diff) | |
| download | MASShortcut-278c5fa2a5f50b1281929179a570403b605015e5.tar.bz2 | |
Switching to NSUserDefaultsController.
NSUserDefaults does not support KVO officially.
| -rw-r--r-- | README.md | 44 | 
1 files changed, 29 insertions, 15 deletions
| @@ -26,26 +26,40 @@ I hope, it is really easy:  To set an example, I made a  demo project: [MASShortcutDemo](https://github.com/shpakovski/MASShortcutDemo). Enjoy!  #Notifications -By registering for NSNotifications from NSUserDefaults observing, you can get a callback whenever a user changes the shortcut, allowing you to perform any UI updates, or other code handling tasks. +By registering for KVO notifications from `NSUserDefaultsController`, you can get a callback whenever a user changes the shortcut, allowing you to perform any UI updates, or other code handling tasks.  This is just as easy to implement: -    //implement when loading view -    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; -    [defaults addObserver:self -               forKeyPath:kPreferenceGlobalShortcut -                  options:NSKeyValueObservingOptionNew -                  context:NULL]; - -    //capture the KVO change and do something -    - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context -    { -        NSLog(@"KVO changed"); +```objective-c +// Declare an ivar for key path in the user defaults controller +NSString *_observableKeyPath; +     +// Make a global context reference +void *kGlobalShortcutContext = &kGlobalShortcutContext; +     +// Implement when loading view +_observableKeyPath = [@"values." stringByAppendingString:kPreferenceGlobalShortcut]; +[[NSUserDefaultsController sharedUserDefaultsController] addObserver:self forKeyPath:_observableKeyPath +                                                             options:NSKeyValueObservingOptionInitial +                                                             context:kGlobalShortcutContext]; + +// Capture the KVO change and do something +- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)obj +                        change:(NSDictionary *)change context:(void *)ctx +{ +    if (ctx == kGlobalShortcutContext) { +        NSLog(@"Shortcut has changed"); +    } +    else { +        [super observeValueForKeyPath:keyPath ofObject:obj change:change context:ctx];      } +} -    //don't forget to remove the observer -    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; -    [defaults removeObserver:self forKeyPath:kPreferenceGlobalShortcut]; +// Do not forget to remove the observer +[[NSUserDefaultsController sharedUserDefaultsController] removeObserver:self +                                                             forKeyPath:_observableKeyPath +                                                                context:kGlobalShortcutContext]; +```  # Non-ARC Version | 
