繁体   English   中英

如何通知对NSObjectController的任何绑定更改

[英]How to be notified of any binding changes to an NSObjectController

我有一个NSView ,其中所有控件都已使用Interface Builder中的NSObjectController绑定到模型对象。

这可以正常工作。 现在,只要这些绑定中的任何一个更改,我都希望通知我的NSViewController 这可能吗? 如果是这样,怎么办?

我最终使用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];

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM