对两个按钮使用相同的pickerView返回空数据

时间:2019-04-01 12:03:22

标签: ios swift pickerview

这是我的代码。我正在尝试将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)

        }
    }
}

2 个答案:

答案 0 :(得分:0)

还需要为pickerViews设置dataSource。因此,调用UIPickerViewDataSource的方法负责将所有数据返回到UIPickerView。您的班级必须确认UIPickerViewDataSource协议。

例如:

picker_country.dataSource = self

答案 1 :(得分:0)

首先,将类中的UIPickerView协议导入为

class SearchBuyVC: UIViewController  UIPickerViewDelegate, UIPickerViewDataSource

并添加以下几行以调用数据源函数,

mainPV.delegate = self
secPV.delegate = self
minPV.delegate = self
mainPV.dataSource = self
secPV.dataSource = self
minPV.dataSource = self