首页 > 解决方案 > UIPickerView 数据未显示在子视图中

问题描述

我有一个容器视图,其中显示了连接选择器视图的文本字段。选择器视图未在容器视图中显示数据

@IBOutlet var txtfield: UITextField!
var pickerView = UIPickerView()

pickerView.delegate = self
        pickerView.dataSource = self

tYear.inputView = pickerViewYear <----- 线程 1:致命错误:在展开可选值时意外发现 nil

标签: iosswiftuipickerview

解决方案


如果我没记错的话,您没有创建选择器视图框架,这就是您崩溃的原因。当您单击 textField 时,请尝试下面的选择器视图和 UITool 代码,它将出现。

    @IBOutlet var txtfield: UITextField!
    var pickerView = UIPickerView()

override func viewDidLoad() {
        super.viewDidLoad()
        setUPPickerView()
    }

创建 UIPickerView

     func setUPPickerView(){
        self.picker = UIPickerView(frame:CGRect(x: 0, y: 0,
                                                width: self.view.frame.size.width,
                                                height: self.view.frame.size.height / 4.2))
        self.picker.delegate = self
        self.picker.dataSource = self

    }
//TextFieldDelegate method as you tap textField, picker view will appear.

extension ViewController: UITextFieldDelegate {

func textFieldDidBeginEditing(_ textField: UITextField) {
    self.showUITool(textField)
}

func showUITool(_ textField : UITextField) {

    for (index, _) in pickerData.enumerated() {
        if pickerData[index] == selectedLangauge {
            picker.selectRow(index, inComponent: 0, animated: true)

        }
    }

    textField.inputView = picker
    // ToolBar
    let toolBar = UIToolbar()
    toolBar.barStyle = .default
    toolBar.isTranslucent = false
    toolBar.barTintColor = // what ever color you want change accordingly 
    toolBar.sizeToFit()

    // Adding Button ToolBar
    let doneButton   = UIBarButtonItem(title: "Done", style: .plain, target: self,
                                       action: #selector(donedatePicker))
    let cancelButton = UIBarButtonItem(title: "Cancel", style: .plain, target: self,
                                       action: #selector(cancelDatePicker))

    doneButton.setTitleTextAttributes([NSAttributedStringKey.foregroundColor:UIColor.white], for: .normal)
    cancelButton.setTitleTextAttributes([NSAttributedStringKey.foregroundColor:UIColor.white], for: .normal)
//Space between buttons in Tool bar, So adding one more button with target and action nil.
    let spaceButton = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)


    toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
    toolBar.isUserInteractionEnabled = true
    textField.inputAccessoryView = toolBar
}


@objc func donedatePicker() {
    self.view.endEditing(true)
}

@objc func cancelDatePicker(){
    self.view.endEditing(true)
}

UIPickerViewDelegate, UIPickerViewDataSource实现选择器视图的所有必需方法

 func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return data.count
}


func numberOfComponents(in pickerView: UIPickerView) -> Int {
    return 1
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {

    return [row]
}

推荐阅读