点击文本字段时未调用 textFieldDidBeginEditing 函数

textFieldDidBeginEditing func is not being called when tapping on a text field

我有一个包含三个视图的项目,它们都是多个文本字段,在点击时调用 UIPickerView。我有两个视图工作得很好,但是当我开始第三个视图时,我无法调用 textFieldDidBeginEditing 函数。我通过在函数的第一行放置一个断点来确认这一点。当我 运行 时,断点永远不会命中,即使我点击视图中的任何字段也是如此。我复制并粘贴了其他视图中的代码,更改了变量名称。

每个文本字段都在 viewDidLoad 方法中初始化为我在前面的代码中定义的 dataPickerView 变量。 dataPickerView.delegate = selfdataPickerView.dataSource = self也是。

这是我认为的相关代码。我确定我缺少一些简单的东西

@IBOutlet var enterDispPhys: UITextField!
@IBOutlet var enterUser: UITextField!
@IBOutlet var enterInsurance: UITextField!
@IBOutlet var enterBodyPart: UITextField!

override func viewDidLoad() {
    super.viewDidLoad()

    self.userLabel.text = userName
    self.teamLabel.text = teamName

    //initialize the inputView of the dispensing physician field to the PickerView
    enterDispPhys.inputView = dataPickerView

    //initialize the inputView of the user name field to the PickerView
    enterUser.inputView = dataPickerView

    //initialize the inputView of the insurance field to the PickerView
    enterInsurance.inputView = dataPickerView

    //initialize the inputView of the body part field to the PickerView
    enterBodyPart.inputView = dataPickerView

    dataPickerView.delegate = self
    dataPickerView.dataSource = self
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func textFieldDidBeginEditing(textField: UITextField) {
    activeDataArray == [] //clear out the clicked field data array
    if textField == enterDispPhys {
        activeDataArray = dispPhys
    } else
        if textField == enterUser {
            activeDataArray = userNameList
    } else
        if textField == enterInsurance {
            activeDataArray = insCode
    } else
        if textField == enterBodyPart {
            activeDataArray = bodyPart
    }
    dataPickerView.reloadAllComponents()
    dataPickerView.hidden = false
    println(activeDataArray)
}

您是否设置了 class 声明来实现文本字段委托?您是否还为每个字段设置了委托?我喜欢使用观察员作为电子邮件字段 bellor,但您也可以将其添加到 viewDidLoad 中,就像在密码字段中一样。

class ViewController: UIViewController, UITextFieldDelegate {
    @IBOutlet weak var email: UITextField!{
        didSet{email.delegate = self}
    }
    @IBOutlet weak var password: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()
        password.delegate = self
    }
}