Pat_Morita
Pat_Morita

Reputation: 3545

NSButton list or unbind existing bindings programmatically

I am on XCode 9.3, Objective-C, OSX not iOS.

I have an NSButton that receives different bindings programmatically on different situations.

Think of it like this

// 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];

At a certain point i need to unbind existing bindings. Is there a way to read existing bindings from that button? Or at least the keys so i can enumerate them to unbind them?

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

Of course i can store all keys while binding them, just thought there might be a more elegant solution.

Upvotes: 1

Views: 129

Answers (1)

Gerd K
Gerd K

Reputation: 3018

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

Upvotes: 1

Related Questions