focusing a text field in swift

后端 未结 2 1899
深忆病人
深忆病人 2021-01-31 07:43

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:

         


        
相关标签:
2条回答
  • 2021-01-31 08:08

    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)
        }
    
    }
    
    0 讨论(0)
  • 2021-01-31 08:23

    theTextFieldYouWant.becomeFirstResponder()

    0 讨论(0)
提交回复
热议问题