2014-04-18 47 views
0

如何編輯Iphone中的UITextFiled,一旦我輸入了某些內容,但我們無法從文本字段進行編輯。 請幫助我。如何在iPhone中編輯UITextFiled ..?

- (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; 
} 

回答

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;  
} 
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; 
}