Reputation: 311
How can I move control to next text field automatically after entering 10 digit.
Upvotes: 0
Views: 1335
Reputation: 82766
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *currentString = [textField.text stringByReplacingCharactersInRange:range withString:string];
int length = [currentString length];
if (length > 10) {
[yourNextTextfield becomeFirstResponder];
return NO; // add the line
}
return YES;
}
choice no-2
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSUInteger newLength = [textField.text length] + [string length] - range.length;
if (newLength >10)
{
[yournextTextfield becomeFirstResponder];
return NO;
}
return YES;
}
Swift
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
var currentString: String = textField.text!.stringByReplacingCharactersInRange(range, withString: string)
var length: Int = currentString.characters.count
if length > 10 {
yourNextTextfield.becomeFirstResponder()
return false
// add the line
}
return true
}
Another Choice
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
var newLength: Int = textField.text!.characters.count + string.characters.count - range.length
if newLength > 10 {
yournextTextfield.becomeFirstResponder()
return false
}
return true
}
Upvotes: 4
Reputation: 104
-(BOOL)textFieldShouldReturn:(UITextField*)textField{
NSInteger nextTag = textField.tag + 1;
// Try to find next responder
UIResponder* nextResponder = [textField.superview viewWithTag:nextTag];
if (nextResponder) {
// Found next responder, so set it.
[nextResponder becomeFirstResponder];
} else {
// Not found, so remove keyboard.
[textField resignFirstResponder];
}
return NO; // We do not want UITextField to insert line-breaks.
}
if you have multiple textfield you can also use this
Upvotes: 2
Reputation: 104
-(BOOL) textFieldShouldReturn:(UITextField*) textField
{
if (textField == txt1){
[txt1 resignFirstResponder];
[txt2 becomeFirstResponder];
}
if (textField == txt2){
[txt2 resignFirstResponder];
}
return YES;
}
Don't forget to add delegate UITextFieldDelegate to your UITextfield
Upvotes: 0