How do I force the NSNumberFormatter for my NSTextField to execute?

I have a sheet which contains, among other things, an NSTextField with an attached standard NSNumberFormatter and an OK button which closes the sheet and processes all the user inputs. I have a
control:didFailToFormatString:errorDescription delegate routine which handles incorrect entries. It puts up an alert panel, then replaces the user's bad entry with the contents before editing began.

If the user enters something which doesn't obey the specified format and range, everything works fine as long as the user signals completion of editing the field by typing "enter", "return" or "tab". The delegate routine gets control, the alert panel comes up, and the bad data gets replaced.

But if the user clicks on the OK button to end the entire sheet with an invalid update to the NSTextField in progress, my delegate routine never gets control. Instead, the bad contents get replaced with a zero and the sheet closes. Zero, although a legal value, is rarely desirable. The user may not realize that he/she made an invalid entry because the sheet closes with no error indication.

In my action routine which responds to OK, I want to force error checking on the NSTextField programmatically, and (if an error is found) abort the completion of the sheet. I cannot figure out how to do this without essentially recoding what the NSNumberFormatter is supposed to do.

I have tried directly calling the action routine for the NSTextField. I have tried using NSApplication's sendAction:to:from routine to initiate the action through the usual channels. I have tried simply using validateEditing on the NSTextField. I have even tried sending makeFirstResponder:nil to the window, hoping that forcing the text field to give up responder status would initiate the error checking. None of these things have any effect at all. The error checking in my code does not take place. But obviously the formatter is getting involved somehow, because the invalid entry turns magically into a 0.

In desperation, I have tried calling the formatter directly
(getObjectValue:forString:errorDescription). I have also tried calling the
control:didFailToFormatString:errorDescription delegate routine directly. These last two attempts resulted in strange and undesirable behavior of the program, but no alert panel appeared.

I am out of ideas. Can anyone suggest anything?

G5 dual 2.0 GHz, Mac OS X (10.3.9)

Posted on Apr 27, 2006 4:44 PM

Reply
1 reply

Apr 28, 2006 7:52 AM in response to Denis Elliott

"In my action routine which responds to OK, I want to force error checking on the NSTextField programmatically, and (if an error is found) abort the completion of the sheet. I cannot figure out how to do this without essentially recoding what the NSNumberFormatter is supposed to do.


You might want to rethink this approach. Rather than trying to stop something you've already started, why not prevent it from even starting in the first place?

For example, if TextEdit, you create a new document, click File > Save As and the Save panel opens. Notice the Cancel and Save buttons in the lower right-hand corner. If you delete all of the text of the document name that's given in the Save As: textfield, which would be an invalid filename, the Save button is disabled until something appropriate is entered. Maybe you could use the same approach with your sheet?

Hope this helps....



Dual 2.7 GHz PowerPC G5 w/ 2.5 GB RAM Mac OS X (10.4.6)

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

How do I force the NSNumberFormatter for my NSTextField to execute?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.