如何将焦点设置到 iPhone 中的下一个 uitextfield?
我有 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];
}
I have 8 uitextfields that are for a password. Each textfield contains one letter from password. When I type one letter and after that when I press the 2nd key then focus moves to next textfield. But I want that, as soon as I finished typing first character, the focus should move to the next text field without pressing other key or pressing tab key. How can I do that? Any tutorial of sample code for that? The code I have used is as below.
- (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];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
希望这有帮助。
斯威夫特 3.2
斯威夫特 4.1
Hope this helps.
Swift 3.2
Swift 4.1
尝试在 ViewDidLoad 中添加通知,
Try this add notification in ViewDidLoad,