NSButton以编程方式列出或取消绑定现有绑定

时间:2018-04-12 13:17:58

标签: objective-c macos cocoa binding

我在XCode 9.3,Objective-C,OSX而不是iOS。

我有一个NSButton,可以在不同情况下以编程方式接收不同的绑定。

想象这个

// Note these values are just for demonstration, in my code they are all dynamic
BOOL requiredState = true;
NSString* state = @"hidden"; // can also be "enabled" or "hidden2"
SomeObject* someObject = [SomeObject new]; // An object that has a value for keyPath key;
NSString* key = @"someValue";

id options = requiredState ? [NSDictionary dictionaryWithObjectsAndKeys:NSNegateBooleanTransformerName, NSValueTransformerNameBindingOption,nil] : nil;

// Bind button
[self.nextButton bind:state toObject:target withKeyPath:key options:options];

在某一点上,我需要取消绑定现有的绑定。有没有办法从该按钮读取现有绑定?或者至少是钥匙,所以我可以列举它们解开它们?

// Idea
[allBindingKeys enumerateObjectsUsingBlock:^(NSString* key, NSUInteger idx, BOOL * _Nonnull stop) {
        [self.nextButton unbind:key];
    }]

当然,我可以在绑定它们时存储所有键,只是认为可能有更优雅的解决方案。

1 个答案:

答案 0 :(得分:1)

for(NSString *binding in self.nextButton.exposedBindings)
{
    [self.nextButton unbind:binding];
}