I have a panel that elaborates on the selected table row. Obviously, the panel’s display often needs updating, and I’ve pushed that task off to an NSWindowController subclass.
I would like the custom NSWindowController to have @propertys for things like the string values in the text fields and the image values in the NSImageViews. Because I need no novel code in the accessors, I want to @synthesize them. Unfortunately (for me), there is no option to use key-paths in @synthesize property=ivar.
The obvious solution is to write my own accessors of the form
- (void)setTitle:(NSString *)title
{
[titleTextField setStringValue:title];
}
- (NSString *)title
{
return [titleTextField stringValue];
}
but I’d rather not have to do that by hand for each of several properties.
Is there a simpler way? Perhaps a generic way to set up a property to forward to a specific property (except objectValue etc. aren’t actually proper properties) of another object?
I don’t think doing any kind of forwarding is going to be useful since the property names on the textfield are going to be different to the ones on your class.
Abstracting away from the textfield by exposing a string property is of course a good idea, but if you want to avoid writing this boiler plate you may find it acceptable to simply expose the text field itself as a property. You can see this in some of Apples APIs, particularly on the iPhone where in SDK 3.0 instead of having properties like
texton a table cell they now havetextLabel. It’s simpler and allows more customization if a callee wants to customize a label or text field in some way.