如何将一个PickerView用于显示不同数据的几个按钮

时间:2019-04-03 04:10:34

标签: ios swift swift4 uipickerview

我在几个按钮上使用了一个UIPickerView,当我按下第一个按钮时,它显示了正确的数据,但是问题是当我按下第二个和第三个按钮时,其余按钮显示了第一个按钮的数据相同,而不是每个按钮连接的数据。我尝试了很多事情,但仍然无法正常工作。我不知道如果有人可以帮助我,会是什么问题

class SearchBuyVC: UIViewController {

    @IBOutlet weak var optionV: UIView!
    @IBOutlet weak var mainPV: UIPickerView!
    @IBOutlet weak var typePV: UIPickerView!
    @IBOutlet weak var PricePV: UIPickerView!
    @IBOutlet weak var priceMaxPV: UIPickerView!
    @IBOutlet weak var buildMinPV: UIPickerView!
    @IBOutlet weak var buildMaxPV: UIPickerView!
    @IBOutlet weak var tenurePV: UIPickerView!

    @IBOutlet weak var doneBtn: UIButton!
    @IBOutlet weak var searchBtn: UIButton!
    @IBOutlet weak var typeBtn: UIButton!
    @IBOutlet weak var minPriceBtn: UIButton!
    @IBOutlet weak var maxPriceBtn: UIButton!
    @IBOutlet weak var buildMinBtn: UIButton!
    @IBOutlet weak var tenureBtn: UIButton!
    @IBOutlet weak var buildMaxBtn: UIButton!
    @IBOutlet weak var bedView: UILabel!
    @IBOutlet weak var bathView: UILabel!


    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 Residential", "All Commerical"]
    let minPriceData = ["Any", "100 k", "200 k", "300 k", "400 k", "500 k", "600 k", "700 k", "800 k", "900 k"]
    let maxPriceData = ["Any", "700 k", "800 k", "900 k", "1 m", "2 m", "4 m", "6 m", "8 m", "10 m"]
    let buildMinData = ["Any", "500", "1,000", "1,500", "2,000", "2,500", "3,000", "3,500"]
    let buildMaxData = ["Any", "4,000", "4,500", "5,000", "6,000", "8,000", "10,000", "20,000"]
    let tenureData = ["Any", "Freehold", "Leasehold", "MalayReserve Land"]


    override func viewDidLoad() {
        super.viewDidLoad()
        setUpNav(arrowType: 2)
        mainPV.delegate = self
        typePV.delegate = self
        PricePV.delegate = self
        priceMaxPV.delegate = self
        buildMinPV.delegate = self
        buildMaxPV.delegate = self
        tenurePV.delegate = self
    }

    override func viewDidAppear(_ animated: Bool) {
        self.navigationController?.setNavigationBarHidden(false, animated: true)
    }
    @IBAction func selectOptionsPressed(_ sender: Any) {
        openOptions()
    }
    @IBAction func typeOptionPressed(_ sender: Any) {
        openOptions()
    }
    @IBAction func minPriceOptionPressed(_ sender: Any) {
        openOptions()
    }
    @IBAction func maxPriceOptionPressed(_ sender: Any) {
        openOptions()
    }
    @IBAction func buildMaxOptionPressed(_ sender: Any) {
        openOptions()

    }
    @IBAction func buildMinOptionPressed(_ sender: Any) {
        openOptions()
    }

    @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
        doneBtn.isHidden = false
        typePV.isHidden = false
        PricePV.isHidden = false
        priceMaxPV.isHidden = false
        buildMinPV.isHidden = false
        buildMaxPV.isHidden = false
        tenurePV.isHidden = false
    }

    func closeOptions(){
        optionV.isHidden = true
        mainPV.isHidden = true
        doneBtn.isHidden = true
        typePV.isHidden = true
        PricePV.isHidden = true
        priceMaxPV.isHidden = true
        buildMinPV.isHidden = true
        buildMaxPV.isHidden = true
        tenurePV.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.tag == 0{
            return searchData.count
        }
        if pickerView.tag == 1{
            return typeData.count
        }
        if pickerView.tag == 2{
            return minPriceData.count
        }
        if pickerView.tag == 3{
            return maxPriceData.count
        }
        if pickerView.tag == 4{
            return buildMinData.count
        }
        if pickerView.tag == 5{
            return buildMaxData.count
        }
        if pickerView.tag == 6{
            return tenureData.count
        }

        return 0

    }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {

        if pickerView.tag == 0{
            return searchData[row]
        }
        if pickerView.tag == 1{
            return typeData[row]
        }
        if pickerView.tag == 2{
            return minPriceData[row]
        }
        if pickerView.tag == 3{
            return maxPriceData[row]
        }
        if pickerView.tag == 4{
            return buildMinData[row]
        }
        if pickerView.tag == 5{
            return buildMaxData[row]
        }

        if pickerView.tag == 6{
            return tenureData[row]
        }

        return ""
    }

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

        if pickerView.tag == 0{
            searchBtn.setTitle(searchData[row], for: .normal)
            if pickerView.tag == 1{
                typeBtn.setTitle(typeData[row], for: .normal)
            }
            if pickerView.tag == 2{
                minPriceBtn.setTitle(minPriceData[row], for: .normal)
            }
            if pickerView.tag == 3{
                maxPriceBtn.setTitle(maxPriceData[row], for: .normal)
            }
            if pickerView.tag == 4{
                buildMinBtn.setTitle(buildMinData[row], for: .normal)
            }
            if pickerView.tag == 5{
                buildMaxBtn.setTitle(buildMaxData[row], for: .normal)
            }
            if pickerView.tag == 6{
                tenureBtn.setTitle(tenureData[row], for: .normal)
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

仅具有一个选择器视图,并在按钮操作上更改数据源。然后重新加载pickerview。

创建selectedBtn变量以存储当前所选按钮引用,并创建tempArr变量以存储当前所选按钮的数据以显示在选择器视图中。

class SearchBuyVC: UIViewController {

    @IBOutlet weak var pickerView: UIPickerView!

    @IBOutlet weak var doneBtn: UIButton!
    @IBOutlet weak var searchBtn: UIButton!
    @IBOutlet weak var typeBtn: UIButton!
    @IBOutlet weak var minPriceBtn: UIButton!
    @IBOutlet weak var maxPriceBtn: UIButton!
    @IBOutlet weak var buildMinBtn: UIButton!
    @IBOutlet weak var tenureBtn: UIButton!
    @IBOutlet weak var buildMaxBtn: UIButton!
    @IBOutlet weak var bedView: UILabel!
    @IBOutlet weak var bathView: UILabel!

    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 Residential", "All Commerical"]
    let minPriceData = ["Any", "100 k", "200 k", "300 k", "400 k", "500 k", "600 k", "700 k", "800 k", "900 k"]
    let maxPriceData = ["Any", "700 k", "800 k", "900 k", "1 m", "2 m", "4 m", "6 m", "8 m", "10 m"]
    let buildMinData = ["Any", "500", "1,000", "1,500", "2,000", "2,500", "3,000", "3,500"]
    let buildMaxData = ["Any", "4,000", "4,500", "5,000", "6,000", "8,000", "10,000", "20,000"]
    let tenureData = ["Any", "Freehold", "Leasehold", "MalayReserve Land"]
    var tempArr = [String]()
    var selectedBtn = UIButton()

    override func viewDidLoad() {
        super.viewDidLoad()

        pickerView.delegate = self
        pickerView.dataSource = self
    }

    @IBAction func allButtionActions(_ sender: UIButton) {
        selectedBtn = sender

        if sender == typeBtn {
            tempArr = typeData
        } else if sender == minPriceBtn {
            tempArr = minPriceData
        } else if sender == maxPriceBtn {
            tempArr = maxPriceData
        } else if sender == buildMinBtn {
            tempArr = buildMinData
        } else if sender == tenureBtn {
            tempArr = tenureData
        } else if sender == buildMaxBtn {
            tempArr = buildMaxData
        }
        pickerView.reloadAllComponents()
        pickerView.isHidden = false

    }
}

extension SearchBuyVC: UIPickerViewDelegate, UIPickerViewDataSource {
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return tempArr.count
    }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return tempArr[row]
    }

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        selectedBtn.setTitle(tempArr[row], for: .normal)
    }
}