如何在键盘

时间:2017-10-17 04:01:11

标签: ios swift3

我有3个textFields

txtField1
  txtField2
  txtField3

用户需要使用键盘上的键(下一个)转到下一个textField

要求:

txtField1将在键盘上具有(下一个)-Key
  键盘上的txtField2将有(下一个)键   txtField3将在键盘上具有(go)-Key

问题:
如何按键盘上的下一个键转到下一个txtField和 在键盘上的按(Go)键上执行验证,并检查txtField是否为空。

My Code :

1. The class conform to UITextFieldDelegate

Class myTestVC : UIViewController, UITextFieldDelegate {}

override fun viewDidLoad() {

  txtField1.delegate = self
  txtField1.tag = 0 
  txtField1.returnKeyType = .next
  txtField1.borderStyle = UITextBorderStyle.none

  txtField2.delegate = self
  txtField2.tag = 1
  txtField2.returnKeyType = .next
  txtField2.borderStyle = UITextBorderStyle.none

  txtField3.delegate = self
  txtField3.tag = 2
  txtField3.returnKeyType = .next
  txtField3.borderStyle = UITextBorderStyle.none

  //- to show the Keyboard 
  txtField1.becomeFirstResponder()


}

2)  How to implement this for the above scenario?

func textFieldShouldReturn( _ textField: UITextField) -> Bool {


}

请帮忙。

由于

2 个答案:

答案 0 :(得分:1)

您可以这样使用:

override func viewDidLoad() {
    super.viewDidLoad()
    txtField1.delegate = self
    txtField1.tag = 0
    txtField1.returnKeyType = .next
    txtField1.borderStyle = UITextBorderStyle.none

    txtField2.delegate = self
    txtField2.tag = 1
    txtField2.returnKeyType = .next
    txtField2.borderStyle = UITextBorderStyle.none

    txtField3.delegate = self
    txtField3.tag = 2
    txtField3.returnKeyType = .go
    txtField3.borderStyle = UITextBorderStyle.none

    //- to show the Keyboard
    txtField1.becomeFirstResponder()
}

func textFieldShouldReturn(_ textField: UITextField) -> Bool
{
    switch textField
    {
        case txtField1:
            txtField2.becomeFirstResponder()
        case txtField2:
            txtField3.becomeFirstResponder()
        default:
            if textField.returnKeyType == .go {
                var txtError = ""
                if(txtField1.text == nil || txtField1.text!.isEmpty) {
                    txtError = "txtField1"
                }
                if(txtField2.text == nil || txtField2.text!.isEmpty) {
                    txtError += "txtField2"
                }
                if(txtField3.text == nil || txtField3.text!.isEmpty) {
                    txtError += "txtField3"
                }

                if txtError.isEmpty {
                    textField.resignFirstResponder()
                } else {
                    let alert = UIAlertController(title: "Alert", message: "\(txtError) are empty", preferredStyle: UIAlertControllerStyle.alert)
                    alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil))
                    self.present(alert, animated: true, completion: nil)
                }
            }
    }
    return true
}

答案 1 :(得分:0)

导入UIKit

class myTestVC:UIViewController,UITextFieldDelegate {

@IBOutlet weak var txtField1: UITextField!

@IBOutlet weak var txtField2: UITextField!

@IBOutlet weak var txtField3: UITextField!
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    self.txtField1.delegate = self
    self.txtField1.tag = 0
    self.txtField1.returnKeyType = .next
    self.txtField1.borderStyle = UITextBorderStyle.none

    self.txtField2.delegate = self
    self.txtField2.tag = 1
    self.txtField2.returnKeyType = .next
    self.txtField2.borderStyle = UITextBorderStyle.none

    self.txtField3.delegate = self
    self.txtField3.tag = 2
    self.txtField3.returnKeyType = .go
    self.txtField3.borderStyle = UITextBorderStyle.none

    //- to show the Keyboard
    self.txtField1.becomeFirstResponder()

}

func textFieldShouldReturn(_ textField:UITextField) - >布尔{

    if (textField.tag == 0){
        self.txtField2.becomeFirstResponder()
    }
    else if (textField.tag == 1){
        self.txtField3.becomeFirstResponder()
    }
    else if (textField.tag == 2){

        if textField.returnKeyType == .go{
            var textValueCheck = ""
            if(txtField1.text!.trimmingCharacters(in: CharacterSet.whitespaces) == "") {
                textValueCheck = "txtField1, "
            }
            if(txtField2.text!.trimmingCharacters(in: CharacterSet.whitespaces) == "") {
                textValueCheck += "txtField2, "
            }
            if(txtField3.text!.trimmingCharacters(in: CharacterSet.whitespaces) == "") {
                textValueCheck += "txtField3"
            }
            if textValueCheck.isEmpty {
                textField.resignFirstResponder()
                // proceed with text field data as all text fields have value.
            } else {
                //show alert for any onr of textfield empty
                let textEmptyAlert = UIAlertController(title: "Text Filed Empty", message: "No data for \(textValueCheck)", preferredStyle: UIAlertControllerStyle.alert)
                textEmptyAlert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
                self.present(textEmptyAlert, animated: true, completion: nil)
            }

        }


    }

    return true

}