Pickerview 在文本字段中第二次单击时显示为空,当我按下完成或取消应用程序崩溃时
Posted
技术标签:
【中文标题】Pickerview 在文本字段中第二次单击时显示为空,当我按下完成或取消应用程序崩溃时【英文标题】:Pickerview is showing empty on second click in the textfield, adn when i press Done or Cancel the App crashes 【发布时间】:2020-07-16 12:30:54 【问题描述】: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】: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()
【讨论】:
好吧,如果我从选择器中选择一个输入,比如女性,文本字段会接受输入,但是当我再次按下时,选择器显示为空,知道修改解决了所有其他问题 :) 我没有注意到我应该添加 _ 但是当我添加它时,文本字段停止响应 让它return true
以上是关于Pickerview 在文本字段中第二次单击时显示为空,当我按下完成或取消应用程序崩溃时的主要内容,如果未能解决你的问题,请参考以下文章
应用程序第二次启动时显示第二个 ViewController