• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
[SOLVED] Re: NSTextFieldCell editing text that is an attribute of the bound object
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[SOLVED] Re: NSTextFieldCell editing text that is an attribute of the bound object


  • Subject: [SOLVED] Re: NSTextFieldCell editing text that is an attribute of the bound object
  • From: Luke Evans <email@hidden>
  • Date: Sat, 31 Jan 2009 03:54:18 -0800


So, I really think there must be a 'proper' way to get the editor to pick up and return the value (name) I need editing from my cell's model object, but I'm still searching for the white magic, having dabbled in the darker sort.



OK, I think I finally (mostly) grokked it.

The NSCell subclass can pretend it was given a string object (the name of the actual bound object) and palm this off on its superclass (NSTextFieldCell). The actual object given can be stashed in 'representedObject', so long as you remember to copy this in copyWithZone: and this can be used to drive all the extras in any custom drawing code for the cell.

You can pick up the string at the end of editing, with the cell's "endEditing:"

So I have code like this:

- (id)copyWithZone:(NSZone *)zone {
    NSCell *newCell = [super copyWithZone:zone];
    [newCell setRepresentedObject:[self representedObject]];
    return newCell;
}

- (void)setObjectValue:(id)object {
    if ([object isKindOfClass:[Thing class]]) {
        [self setRepresentedObject:object];
        [super setObjectValue:[object name]];
    }
}

- (void)endEditing:(NSText *)textObj {
    [super endEditing:textObj];

// Set the representedObject's text
[[self representedObject] setValue:[textObj string] forKey:@"name"];
}


Thankfully there's no messing with (abusing?) NSFormatter subclasses this way,

The only remaining fly in the ointment is that the model object ("Thing" class here, the object stored in representedObject) still gets a KVC message to set the OLD value of cell to the "" (empty string) key, which I currently just defend against, ignoring the value. I'd love to know how to prevent this from happening.

-- lwe

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >NSTextFieldCell editing text that is an attribute of the bound object (From: Luke Evans <email@hidden>)
 >Re: NSTextFieldCell editing text that is an attribute of the bound object - EVIL HACK (From: Luke Evans <email@hidden>)

  • Prev by Date: Reverting Address Book changes
  • Next by Date: Can we render QCRenderer in CAOpenGLLayer..?
  • Previous by thread: Re: NSTextFieldCell editing text that is an attribute of the bound object - EVIL HACK
  • Next by thread: Reverting Address Book changes
  • Index(es):
    • Date
    • Thread