From f01480c2bdb8e966d7ec36913f7e08dc5b69a739 Mon Sep 17 00:00:00 2001 From: michael starke Date: Wed, 24 Aug 2016 16:25:06 +0200 Subject: [PATCH] model updates via binding thourh mpviewcontroller now get registered --- MacPass/MPViewController.h | 21 ++++++++++++++++++++- MacPass/MPViewController.m | 20 ++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/MacPass/MPViewController.h b/MacPass/MPViewController.h index a68c40ab..649d37d8 100644 --- a/MacPass/MPViewController.h +++ b/MacPass/MPViewController.h @@ -14,7 +14,26 @@ - (void)didLoadView; - (NSResponder *)reconmendedFirstResponder; -/* Returns the associated window controller */ - (void)updateResponderChain; + + +#pragma mark Binding Observation +/** + * Override this to get notificied when setValue:forKeyPath will be called with a keypath starting with representedObject. + * This is always called via the binding system, hence it's usefull to anticipate model changes via the ui + * + * The default implementation calls this just befor setValue:forKeyPath: + * + * @param keyPath the full key path about to be affected + */ +- (void)willChangeValueForRepresentedObjectKeyPath:(NSString *)keyPath; +/** + * Override this to get notified when setValue:forKeyPath was called with a keypath starting with representedObject. + * The default implementation calls this right after setValue:forKeyPath: + * + * @param keyPath the full key path affected + */ +- (void)didChangeValueForRepresentedObjectKeyPath:(NSString *)keyPath; + @end diff --git a/MacPass/MPViewController.m b/MacPass/MPViewController.m index 536517b0..71b21c86 100644 --- a/MacPass/MPViewController.m +++ b/MacPass/MPViewController.m @@ -38,4 +38,24 @@ } } +#pragma mark Binding observation +- (void)setValue:(id)value forKeyPath:(NSString *)keyPath { + if([keyPath hasPrefix:@"representedObject."]) { + [self didChangeValueForRepresentedObjectKeyPath:keyPath]; + [super setValue:value forKeyPath:keyPath]; + [self didChangeValueForRepresentedObjectKeyPath:keyPath]; + } + else { + [super setValue:value forKeyPath:keyPath]; + } +} + +- (void)willChangeValueForRepresentedObjectKeyPath:(NSString *)keyPath { + NSLog(@"[%@ willChangeValueForRepresentedObjectKeyPath:%@]", NSStringFromClass([self class]), keyPath); +} + +- (void)didChangeValueForRepresentedObjectKeyPath:(NSString *)keyPath { + NSLog(@"[%@ didChangeValueForRepresentedObjectKeyPath:%@]", NSStringFromClass([self class]), keyPath); +} + @end