gpt4 book ai didi

ios - UIPickerView 行为异常

转载 作者:行者123 更新时间:2023-11-30 12:48:10 25 4
gpt4 key购买 nike

所以我觉得问这个问题很愚蠢,但我根本不知道出了什么问题。基本上,当我单击一个以 UIPickerView 作为其 inputView 的文本字段时,它会很快显示然后消失。但是,工具栏(其 accessoryView 仍然保留在屏幕上)。我在网上没有看到其他人经历过这种情况,所以这就是为什么我不得不在 SO 上询问。

起初我以为这与我设置 pickerView 的 .isHidden 属性有关。但我忽略了这些调用,但没有效果。

因此,我将包含与我的 pickerViews 相关的所有代码,因为我真的不知道问题出在哪里。我确信我缺少一些小东西,但任何帮助将不胜感激。

class myAssessmentsViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate {

@IBOutlet weak var contentSelectionTextField: UITextField!
@IBOutlet weak var contentOrderingTextField: UITextField!

var contentSelectionPickerView: UIPickerView = UIPickerView()
var contentOrderingPickerView: UIPickerView = UIPickerView()

var contentSelectionOptions: [String] = ["All", "Physics HL", "Chemistry HL", "Spanish Ab SL"]
var contentOrderingOptions: [String] = ["Date", "Subject", "Grade", "Title"]

required init?(coder aDecoder: NSCoder) {

super.init(coder: aDecoder)

}

override func viewDidLoad() {
super.viewDidLoad()

contentSelectionPickerView.tag = 1 //for the delegate methods
contentSelectionPickerView.isHidden = true //commenting this out did nothing
contentSelectionPickerView.delegate = self
contentSelectionPickerView.dataSource = self

contentSelectionTextField.inputView = contentSelectionPickerView //set pickerView as responder
contentSelectionTextField.delegate = self

contentOrderingPickerView.tag = 2 //for the delegate methods
contentOrderingPickerView.isHidden = true //commenting this out also did nothing
contentOrderingPickerView.delegate = self
contentOrderingPickerView.dataSource = self

initializePickerViewToolBar(clearButtonFunc: "clearPressedContentSelectionPickerView", doneButtonFunc: "donePressedContentSelectionPickerView", textField: contentSelectionTextField)
initializePickerViewToolBar(clearButtonFunc: "clearPressedContentOrderingPickerView", doneButtonFunc: "donePressedContentOrderingPickerView", textField: contentOrderingTextField)

contentOrderingTextField.inputView = contentOrderingPickerView //set pickerView as responder
contentOrderingTextField.delegate = self


// Do any additional setup after loading the view.
}

func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1 //same for both pickers
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {

if pickerView.tag == 1 { //contentSelectionPickerView
return contentSelectionOptions.count
} else if pickerView.tag == 2 { //contentOrderingPickerView
return contentOrderingOptions.count
} else {
return 1
}

}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {

self.view.endEditing(true)

if pickerView.tag == 1 { //contentSelectionPickerView
return contentSelectionOptions[row]
} else if pickerView.tag == 2 { //contentOrderingPickerView
return contentOrderingOptions[row]
} else {
return "1"
}

}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

if pickerView.tag == 1 {
contentSelectionTextField.text = contentSelectionOptions[row]
} else if pickerView.tag == 2 {
contentOrderingTextField.text = contentOrderingOptions[row]
}

}

func textFieldDidBeginEditing(_ textField: UITextField) {

if textField == contentSelectionTextField {
contentSelectionPickerView.isHidden = false //also was not source of problem
} else if textField == contentOrderingTextField {
contentOrderingPickerView.isHidden = false //same here
}

}

func donePressedContentSelectionPickerView(){
contentSelectionTextField.resignFirstResponder()
}
func donePressedContentOrderingPickerView(){
contentOrderingTextField.resignFirstResponder()
}

func clearPressedContentSelectionPickerView(){
contentSelectionTextField.resignFirstResponder()
contentSelectionTextField.text = ""
}
func clearPressedContentOrderingPickerView(){
contentOrderingTextField.resignFirstResponder()
contentOrderingTextField.text = ""
}

func initializePickerViewToolBar(clearButtonFunc: String, doneButtonFunc: String, textField: UITextField){

let toolBar = UIToolbar(frame: CGRect(x: 0, y: textField.frame.size.height/6, width: textField.frame.size.width, height: 40.0))
toolBar.layer.position = CGPoint(x: textField.frame.size.width/2, y: textField.frame.size.height-20.0)
toolBar.barStyle = .default
toolBar.tintColor = UIColor.black

let clearButton = UIBarButtonItem(title: "Clear", style: .plain, target: self, action: Selector(clearButtonFunc))
let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: Selector(doneButtonFunc))
let flexSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil)

toolBar.setItems([clearButton,flexSpace,doneButton], animated: true)
toolBar.isUserInteractionEnabled = true

textField.inputAccessoryView = toolBar

}

}

这也是我正在谈论的视觉照片。正如您在屏幕底部看到的那样,accessoryView 仍然可见,但内容不可见。我认为 AccessoriesView 将是 UIPickerView 的 subview ,并且它们会一起消失,但显然情况并非如此。

The issue

再次对所有代码(以及大图像)表示歉意,我知道阅读起来有很多内容,但任何见解都将不胜感激!

最佳答案

事实证明,我所关注的 SO 的答案之一告诉我在我的 pickerViewTitleForRow 委托(delegate)方法中包含 self.view.endEditing(true) 。这意味着每当设置标题时,第一响应者就会被解雇,从而导致此问题。删除它导致问题消失。感谢@MikeTaverne 指出这一点。

关于ios - UIPickerView 行为异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41352433/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com