对返回空数据的两个按钮使用相同的pickerView
Posted
技术标签:
【中文标题】对返回空数据的两个按钮使用相同的pickerView【英文标题】:Using same pickerView for two buttons returning empty data 【发布时间】:2019-04-01 12:03:22 【问题描述】:这是我的代码。我正在尝试将UIPickerview
用于具有不同数据的几个按钮。但是当我运行我的项目时,它显示了我的空白灰色选择器视图。有人告诉我我必须重新加载pickerView,但我使用了它,例如“mainPV.delegate = self .....如果有人可以帮助我,请帮助我
import UIKit
class SearchBuyVC: UIViewController
@IBOutlet weak var optionV: UIView!
@IBOutlet weak var doneBtn: UIButton!
@IBOutlet weak var searchBtn: UIButton!
// picker view
@IBOutlet var mainPV: UIPickerView! // main
@IBOutlet var secPV: UIPickerView! //type
@IBOutlet var minPV: UIPickerView! //priceMin
@IBOutlet weak var typeBtn: UIButton!
@IBOutlet weak var PriceMinBtn: UIButton!
var optionSelector = 0
let searchData = ["Search by Areas", "Search by Map", "Search by LRT/MRTs", "Search by Schools", "Search by Propery Name"]
let typeData = ["All Residental", "All Commerical"]
let minNum = ["Any", "100 k", "200 k", "300 k", "400 k", "500 k", "600k ", "700 k"]
override func viewDidLoad()
super.viewDidLoad()
setUpNav(arrowType: 2)
mainPV = UIPickerView()
secPV = UIPickerView()
minPV = UIPickerView()
mainPV.delegate = self
secPV.delegate = self
minPV.delegate = self
// Do any additional setup after loading the view.
override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
//Dispose of any resources that
override func viewDidAppear(_ animated: Bool)
self.navigationController?.setNavigationBarHidden(false, animated: true)
@IBAction func selectOptionsPressed(_ sender: Any)
openOptions()
mainPV.delegate = self
secPV.delegate = self
minPV.delegate = self
@IBAction func typeSelect(_ sender: Any)
openOptions()
mainPV.delegate = self
secPV.delegate = self
minPV.delegate = self
@IBAction func minPriceBtn(_ sender: Any)
openOptions()
mainPV.delegate = self
secPV.delegate = self
minPV.delegate = self
@IBAction func donePressed(_ sender: Any)
switch optionSelector
case 0:
FiltersController.instance.showAreaFilter(nc: self.navigationController!)
case 1:
FiltersController.instance.showMapFilter(nc: self.navigationController!)
case 2:
FiltersController.instance.showTrainStations(nc: self.navigationController!)
case 3:
FiltersController.instance.showSchoolFilter(nc: self.navigationController!)
default:
FiltersController.instance.showNameFilter(nc: self.navigationController!)
closeOptions()
func openOptions()
optionV.isHidden = false
mainPV.isHidden = false
minPV.isHidden = false
doneBtn.isHidden = false
secPV.isHidden = false
func closeOptions()
optionV.isHidden = true
mainPV.isHidden = true
minPV.isHidden = true
doneBtn.isHidden = true
secPV.isHidden = true
@IBAction func nextPressed(_ sender: Any)
ListingController.instance.showBuyListing(nc: self.navigationController!)
extension SearchBuyVC: UIPickerViewDelegate, UIPickerViewDataSource
func numberOfComponents(in pickerView: UIPickerView) -> Int
return 1
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
if pickerView == mainPV
return searchData.count
return 1
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
if pickerView == mainPV
return searchData[row]
if pickerView == secPV
return typeData[row]
if pickerView == minPV
return minNum[row]
return ""
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
optionSelector = row
if pickerView == mainPV
searchBtn.setTitle(searchData[row], for: .normal)
if pickerView == secPV
typeBtn.setTitle(typeData[row], for: .normal)
if pickerView == minPV
PriceMinBtn.setTitle(minNum[row], for: .normal)
【问题讨论】:
mainPV = UIPickerView() secPV = UIPickerView() minPV = UIPickerView() 删除所有这些导致加载空视图和 mainPV.dataSource = self secPV.dataSource = self minPV.dataSource = self 这将显示选择器查看每个中的所有数据 func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int if pickerView == mainPV return searchData.count if pickerView == secPV return typeData.count if pickerView = = minPV 返回 minNum.count 返回 1 。为每个选择器视图清楚地传递每个数据 【参考方案1】:还需要为pickerViews设置dataSource。这样UIPickerViewDataSource
的方法就会被调用,负责将所有数据返回给UIPickerView
。你的班级必须确认UIPickerViewDataSource
协议。
例如:
picker_country.dataSource = self
【讨论】:
【参考方案2】:首先将类中的 UIPickerView 协议导入为,
class SearchBuyVC: UIViewController UIPickerViewDelegate, UIPickerViewDataSource
并添加以下行来调用数据源函数,
mainPV.delegate = self
secPV.delegate = self
minPV.delegate = self
mainPV.dataSource = self
secPV.dataSource = self
minPV.dataSource = self
【讨论】:
以上是关于对返回空数据的两个按钮使用相同的pickerView的主要内容,如果未能解决你的问题,请参考以下文章
如何为所有 PickerViews 创建单个完成和取消按钮?