在Swift 3中使用选择器视图时,文本字段委托方法textFieldDidEndEditing
未触发。如何调用它?没有动态调用委托的按钮。
您可以在下面看到我的代码。
import UIKit
class ThroewViewController: UIViewController,UIPickerViewDelegate,UIPickerViewDataSource,UITextFieldDelegate {
@IBOutlet weak var pickerText: UITextField!
let thePicker = UIPickerView()
var dashTitle_Arr = ["One","Two","Three","Four","Five","Six"]
override func viewDidLoad() {
super.viewDidLoad()
pickerText.delegate = self
thePicker.dataSource = self
thePicker.delegate = self
pickerText.inputView = thePicker
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return dashTitle_Arr .count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return dashTitle_Arr [row] }
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
pickerText.text = dashTitle_Arr [row]
}
func textFieldDidEndEditing(_ textField: UITextField) {
addIntoArray(text: textField.text)
}
func addIntoArray(text: String?) {
guard let text = text, text.characters.count > 0 else {
return
}
dashTitle_Arr.append(text)
thePicker.reloadAllComponents()
}
}
答案 0 :(得分:3)
picker-view
的属性为accessoriesView
。使用tool bar
或done
按钮创建cancel
,并在选择器视图中设置assessoriesView
。
在工具栏button
action
中写下textfield.resignFirstResponder()
之后,textFieldDidEndEditing
委托方法被解雇
对我来说很好。希望它能帮到你。谢谢