如何在一个视图控制器中使用多个UIPickerViews

时间:2019-04-05 02:41:28

标签: swift uipickerview

我正在尝试在1个视图控制器中设置2个UIPickerViews。我找不到能解决我问题的资源。

我尝试遵循How to use 2 UIPickerView's in one View Controller?上Diavel Rider的回答。我从中得到一些错误。我也尝试添加标签,但未能成功完成。我正在使用Swift 4。

这是我的视图控制器的代码:

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

    }


}

我需要在1个视图控制器中使用2个选择器视图。我该怎么做?

2 个答案:

答案 0 :(得分:0)

您应该分离数据源和委托,而不要将其放在视图控制器中。将它们设置为委托和数据源。用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]
    }
}

答案 1 :(得分:0)

您正在引用一个不存在的变量,名为pickerView。您应该将参数名称更新为pickerView,以避免出现问题。

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

与其他委托/数据源方法进行类似的更改。