views:

100

answers:

2

Hello, so here is what I am trying to do. I have a birth date text field, while the user in puts text i want the field to automatically put it in the form YYYY-MM-DD. Basically while the user types it replaces the Y's M's and D's but leaves the hyphen. I am not sure how to go about this i may need a mask or something.

I know this is where some of the formatting can be done

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

Any help would be of much help

A: 

You want an NSDateFormatter. See also the Data Formatting Programming Guide for Cocoa (in the iPhone docs).

Joshua Nozzi
A: 

OK so here is what i have now

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    //Format Date of Birth YYYY-MM-DD
    if(textField == txtDOB)
    {
        if ((txtDOB.text.length == 4)||(txtDOB.text.length == 7))
            //Handle backspace being pressed
            if (![string isEqualToString:@""])
            txtDOB.text = [txtDOB.text stringByAppendingString:@"-"];
        return !([textField.text length]>9 && [string length] > range.length);
    }
    else
        return YES;

}

Only problem is that when i try to back space, it doesn't work my guess is because it is trying to add the dash every time i hit the backspace key. Anyone know a work around?

Matt
See my answer. You're re-inventing the wheel and getting bitten by the fact.
Joshua Nozzi