PDA

View Full Version : iphone:Validating NSTextField / NSFormatter




bymartin
Apr 7, 2008, 01:58 PM
Hi,

I have a UITextField that I'd like to validate the input as currency. I looked into using NSFormatter and was able to create one, but the iphone library does not appear to have the setFormatter method to associate the NSNumberFormatter with the textfield.

Does anyone know how to make this association?

Maybe I should use NSCharacterSet instead and just check to make sure my input is part of a custom set (0-9 and .) using characterIsMember?



springframework
Apr 7, 2008, 02:55 PM
If you implement the UITextFieldDelegate and set the textfield.delegate = ...

then inside the delegate functions you can do any formatting/error processing/screen updates/... that you want.

-(BOOL)textFieldShouldReturn(UITextField *)textField{
if(...)
return YES;
else
return NO;
}


there are other functions that can be defined for UITextFieldDelegate

bymartin
Apr 7, 2008, 03:04 PM
Thanks...I already have that part. My question was how to use NSNumberFormatter - but I can do it manually. I was just trying to figure out how to use this convenience class rather than coding it myself.

bymartin
Apr 7, 2008, 04:18 PM
I have it working...I just made a custom NSCharacterSet and then parse my string and check for membership of each unichar. If it's not a member, I can return NO, display an alert, and have the user enter good input.

Sbrocket
Apr 7, 2008, 04:30 PM
You might want to file a bug report (http://bugreporter.apple.com/) just to let engineering be aware that it's not there for UIControl. It could be an intentional decision or it could just be something that got left out, but no one will ever review it if a bug isn't filed. Radar bugs are how the engineers work.

bymartin
Apr 7, 2008, 04:47 PM
I will file it as a bug. Thanks.