Search code examples
objective-ccocoacocoa-bindings

Binding enabled property of NSTextField to existence of setter?


I'm working on a sheet designed to edit an object who's class could be anything that implementes the protocol "Server". The setter methods for the URL are optional, since some types of endpoints, like Amazon S3, have a fixed URL.

What I'm wondering is: is it possible to bind the "Enabled" property of an NSTextField to the existence of those methods? Or do I have to also implement a BOOL method that returns whether or not the class supports editing the URL?

Any advice is greatly appreciated!
Billy


Solution

  • Cocoa bindings rely on key-value coding and key-value observing. In order to bind the "Enabled" property of an NSTextField, you need to bind to a KVC compliant property: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/KeyValueCoding/Concepts/Compliant.html

    This means you'll need to implement a canEditURL property for your server classes.

    You could implement it in a base class as follows though:

    - (BOOL)canEditURL {
          return [self respondsToSelector:@selector(urlSetterNameHere:)];
    }