ios - Having trouble integrating multiple pickerviews in the same view
问题描述
I am trying to set up two pickerviews on one view, but I don't want to show the wheels, I just want the wheels to pop up when the specific text box is clicked. I can get it to work for one Pickerview, but when I try and add the second, the pickerviews no longer work.
I've tried to use "tags" but since I'm not expicitly using UIPickerView!, I can't use the tags, I don't think.
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate{
@IBOutlet weak var cornerText: UITextField!
@IBOutlet weak var brandText: UITextField!
// PICKER VIEW CODE
let Corner = ["LF", "RF", "RR", "LR", "5th"]
let Brand = ["Brand1", "Brand2", "Brand3", "Brand4", "Brand5"]
func numberOfComponents(in cornerPickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ cornerPickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if cornerText.isEditing{
return Corner.count
}
else if brandText.isEditing{
return Brand.count
}
else {
return 0
}
}
func pickerView(_ cornerPickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if cornerText.isEditing{
return Corner[row]
}
else if brandText.isEditing{
return Brand[row]
}
else {
return nil
}
}
var cornerIndex: Int!
var brandIndex: Int!
func pickerView(_ cornerPickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if cornerText.isEditing{
cornerText.text = Corner[row]
cornerIndex = row
}
else if brandText.isEditing{
brandText.text = Brand[row]
brandIndex = row
}
else{
return
}
}
func createPickerView(){
if cornerText.isEditing{
let cornerPickerView = UIPickerView()
cornerPickerView.delegate = self
cornerText.inputView = cornerPickerView
}
else if brandText.isEditing{
let cornerPickerView = UIPickerView()
cornerPickerView.delegate = self
brandText.inputView = cornerPickerView
}
else {
return
}
}
func dismissPickerView(){
let toolBar = UIToolbar()
toolBar.sizeToFit()
let doneButton = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(self.dismissKeyboard))
toolBar.setItems([doneButton], animated: false)
toolBar.isUserInteractionEnabled = true
if cornerText.isSelected{
cornerText.inputAccessoryView = toolBar
}
else {
brandText.inputAccessoryView = toolBar
}
}
@objc func dismissKeyboard(){
view.endEditing(true)
// Put anything that must pop up based on the corner here
I would like both pickerviews to work, right now neither pickerview's display.
解决方案
Your picker view data source/delegate code looks OK. The problem appears to be with how you create and setup the input view and input accessory view for each text field.
You only need one picker view and one tab bar. Assign each to both text fields.
Remove your dismissPickerView
method and update your createPickerView
method as follows:
func createPickerView() {
let pickerView = UIPickerView()
pickerView.delegate = self
pickerView.dataSource = self
cornerText.inputView = pickerView
brandText.inputView = pickerView
let toolBar = UIToolbar()
let doneButton = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(self.dismissKeyboard))
toolBar.setItems([doneButton], animated: false)
cornerText.inputAccessoryView = toolBar
brandText.inputAccessoryView = toolBar
}
Since both text fields share the same picker view, you will need to reload the picker view each time a text field begins editing. Implement the textFieldDidBeginEditing
delegate method, access the picker view and reload it.
推荐阅读
- javascript - 尽管导入了模块并包含 CUSTOM_ELEMENTS_SCHEMA 模式,但组件不是已知元素
- sql - 需要返回空字符串而不是 0
- python-3.x - 如何使用 pyaudio 播放 wav 二进制数据?
- android - 如何使我的 /data 文件夹从 RO 到 RW?
- excel - 将 VBA 变量条件存储在单元格中
- java - 在 java maven 项目上使用 GraphFames
- java - 自动调整大小和自动换行 JToolbar
- design-patterns - Alfresco的微服务如何访问数据库?
- python - Scrapy:检查页面是否包含 HTML 表单元素
- ios - 为强制升级设置远程禁用时 iOS 应用程序崩溃