Using control:didFailToFormatString:errorDescription:
Using control:didFailToFormatString:errorDescription:
- Subject: Using control:didFailToFormatString:errorDescription:
- From: Rohan Lloyd <email@hidden>
- Date: Mon, 8 Sep 2008 11:23:30 +1000
I have a sheet with some text fields. The values of the textFields are
bound to an NSObjectController. One of the text fields has an
NSNumberFormatter, restricting it to numeric input.
If I type a random string (not a number) in, and tab out of the field,
I get the standard "Format error - 'Discard Change' 'OK'" dialog. When
I select 'OK', the dialog is dismissed, and I am back in my sheet with
the focus in the same text field, and the text field containing the
same (invalid) text.
I don't like the vague "Format Error" and want to display my own
message, so I've written my own
control:didFailToFormatString:errorDescription: method (In my
NSWindowController subclass), and set the text field delegate to point
at it.
- (BOOL)control: (NSControl*)control didFailToFormatString:
(NSString*)str errorDescription: (NSString*)errDescription
{
NSError *error = [NSError errorWithDomain: NSCocoaErrorDomain
code: NSFormattingError userInfo: nil]; // Add custom description later
[control presentError: error modalForWindow: [self window]
delegate: nil
didPresentSelector: nil
contextInfo: nil];
return YES;
}
What happens is that when I tab out of the text field, I now get my
sheet with "Format error" (I'll make it more descriptive later) but
when I dismiss the error, the focus has changed to the next text
field. The text field still contains the invalid text, but the
NSObjectController has a nil value for that key.
This means that what is displayed in the sheet does not match what is
in the NSObjectController.
I've tried specifying a didPresentSelector, and setting the focus back
to the offending text field, which succeeds in moving the focus, but
the text field still displays a value that the NSObjectController has
no knowledge of. When I tab out again, I get no validation error.
Surely I'm missing something obvious here... Any suggestions?
Thanks
_______________________________________________
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