我有一个包含三个 View 的项目,它们都是多个文本字段,在点击时调用 UIPickerView。我有两个 View 工作得很好,但是当我开始第三个 View 时,我无法调用 textFieldDidBeginEditing
函数。我通过在函数的第一行放置一个断点来确认这一点。当我运行时,断点永远不会命中,即使我点击 View 中的任何字段也是如此。我复制并粘贴了其他 View 中的代码,更改了变量名称。
每个文本字段都在 viewDidLoad
方法中初始化为我在之前代码中定义的 dataPickerView 变量。 dataPickerView.delegate = self
和 dataPickerView.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)
}
最佳答案
您是否设置类声明来实现文本字段委托(delegate)?您是否还为每个字段设置了委托(delegate)?我喜欢使用观察者作为电子邮件字段 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
}
}
关于ios - 点击文本字段时未调用 textFieldDidBeginEditing 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30518928/