我有8个用于密码的uitextfields。每个文本字段包含密码中的一个字母。当我输入一个字母后,当我按下第二个键时,焦点移动到下一个文本字段。但我想要的是,一旦我完成第一个字符的输入,焦点应该移动到下一个文本字段而不按其他键或按Tab键。我怎样才能做到这一点?任何示例代码教程?我使用的代码如下。
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(keyPressed:) name: UITextFieldTextDidChangeNotification object: nil];
// Try to find next responder
UIResponder* nextResponder = [textField.superview viewWithTag:nextTag];
if (nextResponder) {
// Found next responder, so set it.
//nextTag += 1;
[nextResponder becomeFirstResponder];
if ([nextResponder tag] == 308) {
//[self performSelector:@selector(checkPassCode) withObject:nil afterDelay:0.0];
[NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(checkPassCode) userInfo:nil repeats:NO];
lastResponder = nextResponder;
}
return YES;
} else {
[textField resignFirstResponder];
return NO;
}
}
-(void) keyPressed: (NSNotification*) notification
{
nextTag += 1;
[[NSNotificationCenter defaultCenter] removeObserver: self name:UITextFieldTextDidChangeNotification object: nil];
}
答案 0 :(得分:13)
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSString *finalString = [textField.text stringByReplacingCharactersInRange:range withString:string];
if ( [finalString length] > 0 ) {
textField.text = string;
UIResponder* nextResponder = [textField.superview viewWithTag:(textField.tag + 1)];
if (nextResponder) {
[nextResponder becomeFirstResponder];
// Your code for checking it.
}
return NO;
}
return YES;
}
希望这有帮助。
Swift 3.2
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let finalString: String? = (textField.text? as NSString).replacingCharacters(in: range, with: string)
if (finalString?.characters.count ?? 0) > 0 {
textField.text = string
let nextResponder: UIResponder? = textField.superview?.viewWithTag((textField.tag + 1))
if nextResponder != nil {
nextResponder?.becomeFirstResponder()
// Your code for checking it.
}
return false
}
return true
}
Swift 4.1
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let finalString: String? = (textField.text as NSString?)?.replacingCharacters(in: range, with: string)
if finalString?.count > 0 {
textField.text = string
let nextResponder: UIResponder? = textField.superview?.viewWithTag(textField.tag + 1)
if nextResponder != nil {
nextResponder?.becomeFirstResponder()
// Your code for checking it.
}
return false
}
return true
}
答案 1 :(得分:6)
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
if (textField ==firstTextField) {
[secondTextField becomeFirstResponder];
}else if ( textField == secondTextField){
[thirdTextField becomeFirstResponder];
}else if ( textField == thirdTextField){
[fourthTextField becomeFirstResponder];
}else{
[textField resignFirstResponder];
}
return YES;
}
答案 2 :(得分:5)
在ViewDidLoad中尝试此添加通知,
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(limitTextField:) name:@"UITextFieldTextDidChangeNotification" object:txtField1];
- (void)limitTextField:(NSNotification *)note {
if(txtField1.tag==1)
{
if ([[txtField1 text] length] > 1)
{
[txtField1 resignFirstResponder];
[txtField2 becomeFirstResponder];
}
}
}