How to use multiple UIPickerViews in one view controller

痴心易碎 提交于 2021-01-28 12:49:32

问题


I am trying to set up 2 UIPickerViews in 1 view controller. I have not been able to find resources that solve my problem.

I have tried following the answer from Diavel Rider on How to use 2 UIPickerView's in one View Controller?. I got some errors from that. I have also tried adding tags but was not able to successfully do that. I am using Swift 4.

Here is the code of my view controller:

import UIKit

class FirstViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {

    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }

    func pickerView(_ foodTypeUIPickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        var countrows : Int = nutritionPickerData.count
        if pickerView == foodTypeUIPickerView { **[Error Here: "Binary operator '==' cannot be applied to operands of type '_' and 'UIPickerView'"]**
            countrows = self.foodTypePickerData.count
        }
        return countrows
    }

    func pickerView(_ foodTypeUIPickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        if pickerView == nutritionUIPickerView { **[Error Here: "Binary operator '==' cannot be applied to operands of type '_' and 'UIPickerView'"]**
            let titleRow = nutritionPickerData[row]
            return titleRow
        } else if pickerView == foodTypeUIPickerView { **[Error Here: "Binary operator '==' cannot be applied to operands of type '_' and 'UIPickerView'"]**
            let titleRow = foodTypePickerData[row]
            return titleRow
        }

        return ""

    }

    @IBOutlet weak var nutritionUIPickerView: UIPickerView!

    @IBOutlet weak var foodTypeUIPickerView: UIPickerView!

    var nutritionPickerData = ["Protein", "Fiber", "Iron", "Fat", "Sodium", "Calcium/Vitamin D", "Energy", "Carbohydrates", "Cholestorol"]
    var foodTypePickerData = ["Fruits", "Vegetables", "Legumes and Beans", "Grain", "Meat", "Dairy"]

    @IBAction func submitUIButton(_ sender: Any) {
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        self.nutritionUIPickerView.delegate = self
        self.nutritionUIPickerView.dataSource = self

        self.foodTypeUIPickerView.delegate = self
        self.foodTypeUIPickerView.dataSource = self

    }


}

I need to use 2 picker views in 1 view controller. How do I do this?


回答1:


You are referencing a non-existent variable named pickerView. You should update the parameter name to pickerView to avoid issues.

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    if pickerView == foodTypeUIPickerView {
        return self.foodTypePickerData.count
    } else {
        return self. nutritionPickerData.count
    }
}

Make a similar change to the other delegate/data source methods.




回答2:


You should separate your data sources and delegates and not put them inside your view controller. Set those as the delegate and datasource. Code written in swift 4.2 xcode 10.1

class ViewController: UIViewController {

    @IBOutlet weak var foodPicker: UIPickerView!
    @IBOutlet weak var nutritionPicker: UIPickerView!

    var foodData = FoodData()
    var nutritionData = NutritionData()


    override func viewDidLoad() {
        super.viewDidLoad()

        foodPicker.delegate = foodData
        foodPicker.dataSource = foodData

        nutritionPicker.delegate = nutritionData
        nutritionPicker.dataSource = nutritionData
    }

    @IBAction func actionButton(_ sender: UIButton) {

        let food = foodPicker.delegate?.pickerView!(foodPicker, titleForRow: foodPicker.selectedRow(inComponent: 0), forComponent: 0)

        let nutrition = nutritionPicker.delegate?.pickerView!(nutritionPicker, titleForRow: nutritionPicker.selectedRow(inComponent: 0), forComponent: 0)

        print("You picked: \(food) and \(nutrition)")
    }    
}

class FoodData: NSObject, UIPickerViewDelegate, UIPickerViewDataSource {
    var foodTypePickerData = ["Fruits", "Vegetables", "Legumes and Beans", "Grain", "Meat", "Dairy"]

    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }

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

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

class NutritionData: NSObject, UIPickerViewDataSource, UIPickerViewDelegate {
    var nutritionPickerData = ["Protein", "Fiber", "Iron", "Fat", "Sodium", "Calcium/Vitamin D", "Energy", "Carbohydrates", "Cholestorol"]

    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }

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

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


来源:https://stackoverflow.com/questions/55527506/how-to-use-multiple-uipickerviews-in-one-view-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!