无法识别同一ViewController中不同视图上的TextField

时间:2015-02-03 05:14:43

标签: objective-c swift

我在一个ViewController上有3个UIViews。每个UIView都有自己的TextFields。当我调用以下代码时,它只适用于第一个UIView

func textFieldShouldReturn(textField: UITextField!) -> Bool {
    //first UIView
    if (textField === usernameRegisterTextField) {
        passwordRegisterTextField.becomeFirstResponder()
    } else if (textField === passwordRegisterTextField) {
        emailRegisterTextField.becomeFirstResponder()
    } else if (textField === emailRegisterTextField) {
        self.view.endEditing(true)
    } 
    // second UIView
    else if (textField === usernameLoginTextField) {
        passwordLoginTextField.becomeFirstResponder()
    } else if (textField === passwordLoginTextField) {
        self.view.endEditing(true)
    } 
    // third UIView
    else if (textField === emailForgotTextField) {
        self.view.endEditing(true)
    }
    return true
}

1 个答案:

答案 0 :(得分:1)

您需要将textfield委托附加到ViewController。

self.textfield1.delegate = self;
self.textfield2.delegate = self;
self.textfield3.delegate = self;

从故事板;右键单击从文本字段拖动到ViewController顶部的黄色圆圈,然后选择委托。