如何在一个视图控制器中使用多个 UIPickerViews [英] How to use multiple UIPickerViews in one view controller

查看:23
本文介绍了如何在一个视图控制器中使用多个 UIPickerViews的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 1 个视图控制器中设置 2 个 UIPickerView.我一直无法找到解决我的问题的资源.

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

我已经尝试在 如何遵循 Diavel Rider 的回答在一个视图控制器中使用 2 个 UIPickerView?.我从中得到了一些错误.我也尝试添加标签,但未能成功.我使用的是 Swift 4.

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

    }


}

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

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

推荐答案

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

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.

这篇关于如何在一个视图控制器中使用多个 UIPickerViews的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆