ios 快速聚焦文本字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30913917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 06:29:16  来源:igfitidea点击:

focusing a text field in swift

iosswifttextfield

提问by user2363025

I have 4 textfields on a register screen and i have it set up so that when the user presses next on each text field, the next text field is focused. Code below:

我在注册屏幕上有 4 个文本字段,并且我对其进行了设置,以便当用户在每个文本字段上按 next 时,下一个文本字段会被聚焦。代码如下:

func textFieldShouldReturn(textField: UITextField) -> Bool {
        if (textField == self.fNameField) {
            textField.resignFirstResponder()
            self.sNameField.becomeFirstResponder()
        }
        else if (textField == self.sNameField) {
            self.emailField.becomeFirstResponder()

        } else if (textField == self.emailField) {
            self.passwordField.becomeFirstResponder()
        }
        else{
            var thereWereErrors = checkForErrors()
            if !thereWereErrors
            {
                //conditionally segue to next screen
            }
        }

        return true
    }

On the return of the final text field, I am calling a check for errors function (below). Within that if there is an issue with any field I want to focus that text field so the user can easily correct it. What is happening is that the text field with the error is focusing (as instructed by the checkForErrors functions)for a second but then the focus is switching back to the password text field. I also tried adding in self.passwordField.resignFirstResponder()into the last else of the above function and that makes the password field lose focus but then the text field with the issue encountered is not gaining focus at all (not even for a second as before) How can I fix this?

在返回最终文本字段时,我调用了错误检查函数(如下)。在此范围内,如果任何字段存在问题,我想关注该文本字段,以便用户可以轻松更正它。发生的事情是,带有错误的文本字段正在聚焦(按照 checkForErrors 函数的指示)一秒钟,但随后焦点又切换回密码文本字段。我还尝试添加self.passwordField.resignFirstResponder()到上述函数的最后一个 else 中,这会使密码字段失去焦点,但是遇到问题的文本字段根本没有获得焦点(甚至像以前一样没有一秒钟)我该如何解决这个问题?

func checkForErrors() -> Bool
    {
        var errors = false
        let title = "Error"
        var message = ""
        if fNameField.text.isEmpty {
            errors = true
            message += "First name empty"
            alertWithTitle(title, message: message, ViewController: self)
            self.fNameField.becomeFirstResponder()
        }
        else if sNameField.text.isEmpty
        {
            errors = true
            message += "Surname empty"
            alertWithTitle(title, message: message, ViewController: self)
            self.sNameField.becomeFirstResponder()
        }
        else if emailField.text.isEmpty
        {
            errors = true
            message += "Email empty"
            alertWithTitle(title, message: message, ViewController: self)
            self.emailField.becomeFirstResponder()
        }
        else if !isValidEmail(emailField.text)
        {
            errors = true
            message += "Invalid Email Address"
            alertWithTitle(title, message: message, ViewController: self)
            self.emailField.becomeFirstResponder()
        }
        else if passwordField.text.isEmpty
        {
            errors = true
            message += "Password empty"
            alertWithTitle(title, message: message, ViewController: self)
            self.passwordField.becomeFirstResponder()
        }
        else if count(passwordField.text.utf16)<8
        {
            errors = true
            message += "Password must be at least 8 characters"
            alertWithTitle(title, message: message, ViewController: self)
            self.passwordField.becomeFirstResponder()
        }

        return errors
    }

Note I have included the textField delegate.

注意我已经包含了 textField 委托。

alert with title function as requested:

根据要求具有标题功能的警报:

func alertWithTitle(title: String!, #message: String, #ViewController: UIViewController) {
    let alert = UIAlertController(title: title, message: message, preferredStyle: .Alert)
    let action = UIAlertAction(title: "OK", style: .Cancel, handler: nil)
    alert.addAction(action)
    ViewController.presentViewController(alert, animated: true, completion: nil)
}

回答by user23

theTextFieldYouWant.becomeFirstResponder()

theTextFieldYouWant.becomeFirstResponder()

回答by Juan Carlos Ospina Gonzalez

This works for me:

这对我有用:

import UIKit

class ViewController:UIViewController, UITextFieldDelegate {

    @IBOutlet weak var fNameField: UITextField!
    @IBOutlet weak var sNameField: UITextField!
    @IBOutlet weak var emailField: UITextField!
    @IBOutlet weak var passwordField: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()

        fNameField.delegate = self
        sNameField.delegate = self
        emailField.delegate = self
        passwordField.delegate = self
    }

    func isValidEmail (test:String) ->Bool{
        // your email validation here...
        return true
    }

    func textFieldShouldReturn(textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        if (textField == self.fNameField) {
            self.sNameField.becomeFirstResponder()
        }
        else if (textField == self.sNameField) {
            self.emailField.becomeFirstResponder()

        } else if (textField == self.emailField) {
            self.passwordField.becomeFirstResponder()
        }
        else{
            var thereWereErrors = checkForErrors()
            if !thereWereErrors
            {
                //conditionally segue to next screen
            }
        }

        return true
    }

    func checkForErrors() -> Bool
    {
        var errors = false
        let title = "Error"
        var message = ""
        if fNameField.text.isEmpty {
            errors = true
            message += "First name empty"
            alertWithTitle(title, message: message, ViewController: self, toFocus:self.fNameField)

        }
        else if sNameField.text.isEmpty
        {
            errors = true
            message += "Surname empty"
            alertWithTitle(title, message: message, ViewController: self, toFocus:self.sNameField)

            self.sNameField.becomeFirstResponder()
        }
        else if emailField.text.isEmpty
        {
            errors = true
            message += "Email empty"
            alertWithTitle(title, message: message, ViewController: self, toFocus:self.emailField)

        }
        else if !isValidEmail(emailField.text)
        {
            errors = true
            message += "Invalid Email Address"
            alertWithTitle(title, message: message, ViewController: self, toFocus:self.emailField)

        }
        else if passwordField.text.isEmpty
        {
            errors = true
            message += "Password empty"
            alertWithTitle(title, message: message, ViewController: self, toFocus:passwordField)
        }
        else if count(passwordField.text.utf16)<8
        {
            errors = true
            message += "Password must be at least 8 characters"
            alertWithTitle(title, message: message, ViewController: self, toFocus:self.passwordField)
        }

        return errors
    }

    func alertWithTitle(title: String!, message: String, ViewController: UIViewController, toFocus:UITextField) {
        let alert = UIAlertController(title: title, message: message, preferredStyle: .Alert)
        let action = UIAlertAction(title: "OK", style: UIAlertActionStyle.Cancel,handler: {_ in
            toFocus.becomeFirstResponder()
        });
        alert.addAction(action)
        ViewController.presentViewController(alert, animated: true, completion:nil)
    }

}