Re: Scared by implicit use of 'description' in bindings
Re: Scared by implicit use of 'description' in bindings
- Subject: Re: Scared by implicit use of 'description' in bindings
- From: Paul Lynch <email@hidden>
- Date: Tue, 4 Apr 2006 17:18:09 +0100
On 4 Apr 2006, at 17:09, Matt Gough wrote:
One of the first ArrayControllers that I wrote is [still] being
used by the contentValues binding of an NSMatrix to set the titles
of a bunch of button cells. The controllerKey is arrangedObjects
and the model key path is empty.
Originally the objects in the array controller were just strings.
Everything magically worked (I was new to bindings remember) and my
buttons displayed correctly.
As my code evolved I needed to change the underlying objects being
controlled by the array controller to my own object type. In order
to get my buttons to display correctly I found that I needed to
write a -(NSString*) description method in my class. This also
worked great, but I was a little apprehensive at the time as up
until then I had only really used description for debugging
purposes. Also, at no point in my bindings setup do I ever
explicitly set 'description' as the Model Key path. However, I left
it alone and carried on with more important things.
At some point you bound something (presumably your button title) to
the object itself (selectedObject, or whatever). It will call the
description method to 'coerce' it to a string - this is what you have
missed. Change that binding to your buttonTitle method.
Today I needed to change my buttons so that they displayed an
attributed string instead of a plain string. Just to get it
working, I changed my description method so that it returns an
attributed string instead of a string. Once again it magically
worked, but now I was a little worried. After all, description is
supposed to return an NSString*. The button cell is smart enough to
know what to do with such an return value, but I fear that I am
storing up a whole heap of trouble doing this.
At least you knew it :-).
So then I thought - Hey, why not just create a new method
(attributedStringForButtonTitle) in my class and set that as my
Model key path. But when I do this, my button titles end up showing
a portion of the result of calling 'description' on the returned
attributed string! e.g 'LineHeight 0/0' or '-1, HyphenationFactor'.
I didn't think buttons could take attributed strings as titles, just
plain strings. I could be wrong, but I'm too lazy to look it up. If
I'm right, you need to use an NSString.
For now I have reverted back to using an empty Model Key path and
getting my description method to just call
attributedStringForButtonTitle.
Since originally implementing this array controller, I have gone on
to do many others without getting myself as confused as I am now. I
was beginning to think I understood the ins-and-outs of bindings.
What part of the puzzle am I missing?
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden