1

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

        }
    }
}
4

2 回答 2

0

还需要为pickerViews 设置dataSource。这样UIPickerViewDataSource被调用的方法负责将所有数据返回给UIPickerView. 你的班级必须确认UIPickerViewDataSource协议。

前任:

picker_country.dataSource = self
于 2019-04-01T12:13:00.747 回答
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
于 2019-04-01T12:24:44.633 回答