简体   繁体   中英

UIPickerView data not showing in a subview

I have a container view in which I am showing text field in which picker view is connected. The picker view does not showing data in container view

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

pickerView.delegate = self
        pickerView.dataSource = self

tYear.inputView = pickerViewYear <----- Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

If I am not wrong you are not creating picker view frame that is the reason you are getting crash. Try below code for picker view and UITool when you click textField it will appear.

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

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

Creating 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)
}

Implement all required UIPickerViewDelegate, UIPickerViewDataSource methods of picker view

 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]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM