如何将焦点设置到 iPhone 中的下一个 uitextfield?

发布于 2024-11-09 08:05:37 字数 1327 浏览 0 评论 0原文

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

缺⑴份安定 2024-11-16 08:05:37
- (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;
}

希望这有帮助。

斯威夫特 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
}

斯威夫特 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
}
- (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;
}

Hope this helps.

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
}
情场扛把子 2024-11-16 08:05:37
-(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;
}
-(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;
}
尹雨沫 2024-11-16 08:05:37

尝试在 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];
        }
    }
}

Try this add notification in 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];
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文