开发者

Format Text Field While User Enters Data

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 formattin开发者_开发问答g can be done

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

Any help would be of much help


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


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜