如何在Iphone中编辑文本字段..?

时间:2014-04-18 10:25:37

标签: ios4

如果我输入了某些东西但是我们无法在文本字段中编辑它,如何在Iphone中编辑文本字段。 请帮帮我。

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if([tfieldDOB.text length] == 4)
    {
        tfieldDOB.text=[NSString stringWithFormat:@"%@/",tfieldDOB.text];
    }
    else if([tfieldDOB.text length]==7)
    {
        tfieldDOB.text=[NSString stringWithFormat:@"%@/",tfieldDOB.text];
    }
    return YES;
}

2 个答案:

答案 0 :(得分:0)

您必须使用UITextFieldDelegate。

@interface KInitUserViewController : UIViewController <UITextFieldDelegate>

实施代表......

// Delegate UITextField
- (BOOL)disablesAutomaticKeyboardDismissal {
    return NO;
}

-(BOOL) textFieldShouldReturn:(UITextField *)textField {

    [textField resignFirstResponder];

    //here you can submit your changes....    
    return YES;    
}

答案 1 :(得分:0)

尝试以下适用于您的代码。

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

    if (textField == tfieldDOB) {

        if ([string isEqualToString:@""]) {// This codition for while taping back space on the keyboard
            return YES;
        }
        NSString *currentText = [textField.text stringByReplacingCharactersInRange:range withString:string];
        if (currentText.length == 4 || currentText.length == 7) {
            textField.text = [NSString stringWithFormat:@"%@/",currentText];
            return NO;
        }

    }
    return YES;
}