具有多个文本字段输入视图的多个选择器视图 Swift [英] Multiple pickerviews with multiple textfield inputviews Swift

查看:16
本文介绍了具有多个文本字段输入视图的多个选择器视图 Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在论坛上搜索,但没有任何帮助.我在一个视图控制器中使用了 4 个文本字段,对于每个文本字段,我使用一个单独的 pickerView 作为 textFields (4pickers) 的 inputViews.

I've been searching through the forum and nothing has helped. I am using 4 text fields in one view controller, and for each textfield, I am using a separate pickerView as the inputViews for the textFields (4pickers).

当我点击第一个 textField 时,pickerView1 成功出现并且 textfield 显示数据,但是当我点击第二个、第三个和第四个 textfield 时,第一个 pickerView 出现.我怀疑错误在于 inputView 声明.

When I click on the first textField, pickerView1 successfully appears and the textfield displays the data, however when I click on the second, third and fourth textfields,the first pickerView appears. I suspect the error lies in the inputView declarations.

如果您能帮助在 pickerView 中添加一个完成"按钮,我将不胜感激.

And I would so appreciate it if you could help add a "done" button to the pickerView.

我的代码:

class ViewController1: UIViewController, UIPickerViewDelegate
{

    @IBOutlet var pickerView1: UIPickerView!
    @IBOutlet var pickerView2: UIPickerView!
    @IBOutlet var pickerView3: UIPickerView!
    @IBOutlet var pickerView4: UIPickerView!

    @IBOutlet var textField1: UITextField!
    @IBOutlet var textField2: UITextField!
    @IBOutlet var textField3: UITextField!
    @IBOutlet var textField4: UITextField!

    var hazards = ["a","b", "c"]
    var reasons = ["d", "e", "f"]
    var site = ["v","h","i","j"]
    var line = ["k", "l","m", "n"]

    override func viewDidLoad() {

        super.viewDidLoad()
        pickerView1 = UIPickerView()
        pickerView2 = UIPickerView()
        pickerView3 = UIPickerView()
        pickerView4 = UIPickerView()
        pickerView1.delegate = self
        pickerView2.delegate = self
        pickerView3.delegate = self
        pickerView4.delegate = self

        self.textField1.inputView = self.pickerView1;
        self.textField2.inputView = self.pickerView2;
        self.textField3.inputView = self.pickerView3;
        self.textField4.inputView = self.pickerView4;
    }

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

    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {

        if pickerView.tag == 0 {
            return hazards.count
        } else if pickerView.tag == 1 {
            return reasons.count
        } else if pickerView.tag == 2 {
            return  site.count
        } else if  pickerView.tag == 3 {
            return line.count
        }
        return 1
    }

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

        if pickerView.tag == 0 {
            return hazards[row]
        } else if pickerView.tag == 1 {
            return reasons[row]
        } else if pickerView.tag == 2 {
            return site[row]
        } else if pickerView.tag == 3 {
            return line[row]
        }

        return ""
    }

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

        if pickerView.tag == 0 {
            textField1.text = hazards[row]
        } else if pickerView.tag == 1 {
            textField2.text = reasons[row]
        } else if pickerView.tag == 2 {
            textField3.text = site[row]
        } else if pickerView.tag == 3 {
            textField4.text = line[row]
        }
    }
}

推荐答案

您正在使用选择器视图的 tag 属性来决定哪个数组是给定选择器视图的数据源,但是您最初没有设置标签.tag 默认为零,因此所有四个选择器视图都显示相同的数据.实例化选择器视图后,添加以下内容:

You're using the picker views' tag property to decide which array is the data source for a given picker view, but you didn't set the tags initially. The tag defaults to zero, so all four picker views are showing the same data. After instantiating your picker views, add this:

pickerView1.tag = 0
pickerView2.tag = 1
pickerView3.tag = 2
pickerView4.tag = 3

这篇关于具有多个文本字段输入视图的多个选择器视图 Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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