iphone:Validating NSTextField / NSFormatter

Discussion in 'Mac Programming' started by bymartin, Apr 7, 2008.

  1. macrumors newbie

    Joined:
    Jul 14, 2003
    #1
    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?
     
  2. macrumors member

    Joined:
    Mar 31, 2008
    #2
    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
     
  3. thread starter macrumors newbie

    Joined:
    Jul 14, 2003
    #3
    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.
     
  4. thread starter macrumors newbie

    Joined:
    Jul 14, 2003
    #4
    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.
     
  5. macrumors 65816

    Sbrocket

    Joined:
    Jun 3, 2007
    Location:
    /dev/null
    #5
    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.
     
  6. thread starter macrumors newbie

    Joined:
    Jul 14, 2003
    #6
    I will file it as a bug. Thanks.
     

Share This Page