UIPickerView表现不正常

时间:2016-12-27 21:28:26

标签: ios swift uitextfield uipickerview

所以我觉得这个问题很愚蠢,但我根本无法弄清楚出了什么问题。基本上,当我单击一个具有UIPickerView作为其inputView的textField时,它将很快显示然后消失。但是,toolBar(其accessoryView仍然保留在屏幕上)。我没见过网上有其他人经历过这个,所以这就是为什么我不得不问这个原因。

起初我认为在设置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仍然可见,但内容却不可见。我认为accessoryView将是UIPickerView的子视图,并且它们将一起消失,但显然不是这种情况。

The issue

再一次,为所有代码(以及大图片)道歉,我知道要阅读很多,但任何见解都会非常感激!

1 个答案:

答案 0 :(得分:0)

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