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

时间:2015-05-28 23:50:38

标签: ios swift uitextfield

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

每个textField都在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)
}

1 个答案:

答案 0 :(得分:8)

您是否设置了类声明来实现文本字段委托?你也为每个领域设置了代表吗?我喜欢使用观察者作为电子邮件字段,但您也可以像在密码字段中一样在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
    }
}