Pickerview 在文本字段中第二次单击时显示为空,当我按完成或取消应用程序崩溃时
Pickerview is showing empty on second click in the textfield, adn when i press Done or Cancel the App crashes
var gender = ["MALE","FEMALE", "Other"]
var genderPicker: UIPickerView! = UIPickerView()
// 第一次单击文本字段时,性别选择器视图工作正常,但如果我按下然后再次按下性别文本字段,选择器显示为空。
// 当我按下取消或完成关闭选择器时,应用程序崩溃并出现以下错误
swift
中发送到实例的无法识别的选择器
override func viewDidLoad() {
super.viewDidLoad()
tableViewHeight.constant = screenHeight * 0.7
// Do any additional setup after loading the view.
let toolBar = UIToolbar()
toolBar.barStyle = UIBarStyle.default
toolBar.isTranslucent = true
toolBar.tintColor = UIColor.systemBlue
toolBar.sizeToFit()
let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItem.Style.plain, target: self, action: "donePicker")
let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace, target: nil, action: nil)
let cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItem.Style.plain, target: self, action: "donePicker")
toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
toolBar.isUserInteractionEnabled = true
genderTxtField.delegate = self
genderPicker!.delegate = self
genderPicker!.dataSource = self
genderTxtField.inputAccessoryView = toolBar
self.genderTxtField.inputView = genderPicker
}
func donePicker() {
genderTxtField.resignFirstResponder()
}
// 委托方法
extension InsuranceMethodsSemiViewController: UITextFieldDelegate {
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
genderPicker.isHidden = false
genderTxtField.inputView = genderPicker;
return false
}
func textFieldDidEndEditing(_ textField: UITextField) {
genderPicker.isHidden = true
}
}
extension InsuranceMethodsSemiViewController: UIPickerViewDelegate {
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return gender[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
genderTxtField.text = gender[row]
self.view.endEditing(true)
pickerView.isHidden = true
}
}
extension InsuranceMethodsSemiViewController: UIPickerViewDataSource {
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return gender.count
}
}
1- 添加 _
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
genderPicker.isHidden = false
genderTxtField.inputView = genderPicker;
return true
}
2- 将操作更改为 #selector(donePicker)
let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItem.Style.plain, target: self, action: #selector(donePicker))
let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace, target: nil, action: nil)
let cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItem.Style.plain, target: self, action: #selector(donePicker))
有
@objc func donePicker() {
var gender = ["MALE","FEMALE", "Other"]
var genderPicker: UIPickerView! = UIPickerView()
// 第一次单击文本字段时,性别选择器视图工作正常,但如果我按下然后再次按下性别文本字段,选择器显示为空。
// 当我按下取消或完成关闭选择器时,应用程序崩溃并出现以下错误
swift
中发送到实例的无法识别的选择器override func viewDidLoad() {
super.viewDidLoad()
tableViewHeight.constant = screenHeight * 0.7
// Do any additional setup after loading the view.
let toolBar = UIToolbar()
toolBar.barStyle = UIBarStyle.default
toolBar.isTranslucent = true
toolBar.tintColor = UIColor.systemBlue
toolBar.sizeToFit()
let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItem.Style.plain, target: self, action: "donePicker")
let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace, target: nil, action: nil)
let cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItem.Style.plain, target: self, action: "donePicker")
toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
toolBar.isUserInteractionEnabled = true
genderTxtField.delegate = self
genderPicker!.delegate = self
genderPicker!.dataSource = self
genderTxtField.inputAccessoryView = toolBar
self.genderTxtField.inputView = genderPicker
}
func donePicker() {
genderTxtField.resignFirstResponder()
}
// 委托方法
extension InsuranceMethodsSemiViewController: UITextFieldDelegate {
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
genderPicker.isHidden = false
genderTxtField.inputView = genderPicker;
return false
}
func textFieldDidEndEditing(_ textField: UITextField) {
genderPicker.isHidden = true
}
}
extension InsuranceMethodsSemiViewController: UIPickerViewDelegate {
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return gender[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
genderTxtField.text = gender[row]
self.view.endEditing(true)
pickerView.isHidden = true
}
}
extension InsuranceMethodsSemiViewController: UIPickerViewDataSource {
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return gender.count
}
}
1- 添加 _
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
genderPicker.isHidden = false
genderTxtField.inputView = genderPicker;
return true
}
2- 将操作更改为 #selector(donePicker)
let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItem.Style.plain, target: self, action: #selector(donePicker))
let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace, target: nil, action: nil)
let cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItem.Style.plain, target: self, action: #selector(donePicker))
有
@objc func donePicker() {