UIPickerView 数据未显示在子视图中
UIPickerView data not showing in a subview
我有一个容器视图,在其中显示连接了选取器视图的文本字段。选择器视图不在容器视图中显示数据
@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
如果我没记错的话,您没有创建选择器视图框架,这就是您崩溃的原因。当您单击文本字段时,请尝试选择器视图和 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]
}
我有一个容器视图,在其中显示连接了选取器视图的文本字段。选择器视图不在容器视图中显示数据
@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
如果我没记错的话,您没有创建选择器视图框架,这就是您崩溃的原因。当您单击文本字段时,请尝试选择器视图和 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]
}