2014-01-10 1 views
1

NSView이 있는데 인터페이스 작성기에서 NSObjectController을 사용하여 모든 컨트롤이 모델 객체에 바인딩되어 있습니다.NSObjectController에 대한 바인딩 변경 알림 방법

올바르게 작동합니다. 이제 NSViewController의 변경 사항이있을 때마다 알림을 받겠습니다. 해당 바인딩이 있습니다. 이것이 가능한가? 그렇다면 어떻게?

답변

0

나는 KVO를 사용하여 내 모델 클래스의 멤버를 관찰했다. 프로세스를 자동화하여 (각 모델의 각 구성원마다 이렇게 할 코드를 작성할 필요가 없도록)이 작업을 수행했습니다.

static void *myModelObserverContextPointer = &myModelObserverContextPointer; 

- (void)establishObserversForPanelModel:(FTDisclosurePanelModel *)panelModel { 

    // Add observers for all the model's class members. 
    // 
    // The member variables are updated automatically using bindings as the user makes 
    // adjustments to the user interface. By doing this we can therefore be informed 
    // of any changes that the user is making without having to have a target action for 
    // each control. 

    unsigned int count; 
    objc_property_t *props = class_copyPropertyList([panelModel class], &count); 

    for (int i = 0; i < count; ++i){ 
     NSString *propName = [NSString stringWithUTF8String:property_getName(props[i])]; 
     [panelModel addObserver:self forKeyPath:propName options:0 context:&myModelObserverContextPointer]; 
    } 
} 

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { 

    // Check for insertions/deletions to the model 

    if (context == myModelObserverContextPointer) { 
     if ([_delegate respondsToSelector:@selector(changeMadeToPanelModel:keyPath:)]) { 
      [_delegate changeMadeToPanelModel:object keyPath:keyPath]; 
     } 
    } 
    else 
     [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; 

}