Re: [SOLVED] NSTextField: edits are committed even when -control:isValidObject: returns NO
Re: [SOLVED] NSTextField: edits are committed even when -control:isValidObject: returns NO
- Subject: Re: [SOLVED] NSTextField: edits are committed even when -control:isValidObject: returns NO
- From: Matthew LeRoy <email@hidden>
- Date: Thu, 11 Jul 2013 21:04:00 +0000
- Thread-topic: [SOLVED] NSTextField: edits are committed even when -control:isValidObject: returns NO
Boy, do I feel like a dummy.
While investigating when setObjectValue: is getting called on the cell, I set a breakpoint in my override of that method and decided to check the call stack to see who was calling it -- assuming that it was the text field itself in its implementation of textShouldEndEditing: as I had guessed previously. But when I looked, I saw the following stack:
[TestTextFieldCell setObjectValue:]
[NSCell setStringValue:]
[NSCell stringValue]
[MyViewController control:isValidObject:]
...
Turns out that rather than validating the object passed to me in control:isValidObject:, I was stupidly getting the stringValue directly from the text field and validating that -- and apparently asking for the stringValue causes the cell to retrieve the current edited text from the field editor and call setStringValue: on itself with that text before returning it.
I fixed my code to validate the object passed in isValidObject instead, and now the pending edit is not committed to the cell when I return NO -- setObjectValue: doesn't get called. As a result, if the user enters invalid text and presses tab/return/enter and then unchecks the checkbox, the text field reverts back to the previous valid value as desired.
Thanks to those who took time to pass along their thoughts. My apologies for boring the list with problems caused by my own buggy code :-P
_______________________________________________
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