如何在Swift 3中的同一个控制器中放置2个Pick Views

时间:2017-07-20 16:40:17

标签: ios swift uipickerview

我试图在一个视图控制器中放置2个选择视图。我尝试为它们设置不同的标签。但是这两种观点都有"选择"数组在其中。我控制 - 拖动它们并设置为数据源和委托。我也试过下面的代码。提前谢谢。

import Foundation
import UIKit

class SecondViewController: UIViewController, UIPickerViewDelegate,    UIPickerViewDataSource {
    var model =  Model()
    @IBOutlet weak var hoursTakenText: UITextField!
    @IBOutlet weak var PickerView2: UIPickerView!
    @IBOutlet weak var pickerView1: UIPickerView!
    let choices2 = ["0","0.7","1.0"]
    let choices: [String] = (1...90).map {  String($0)}
    @IBAction func hoursTaken(_ sender: Any) {
        subView.isHidden = false
    }
    @IBOutlet weak var subView: UIView!
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        if pickerView == pickerView1 {
            return choices[row]}
        else {
            return choices2[row]
        }
    }

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        if pickerView == pickerView1 {
            return choices.count}
        else{
            return choices2.count
        }
    }
    //choices[row]: [String] = (1...10).map { "Option " + String(format: "%2d", $0)}
    func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
        let label = (view as? UILabel) ?? UILabel()
        label.font = UIFont(name: "Verdana", size: 15)
        //label.textColor = .blue
        label.textAlignment = .center
        //label.font = UIFont.systemFont(ofSize: 20)
        label.text = choices[row]
        return label
    }
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        let hourstaken = Double(choices[row])
        print(hourstaken!)
        var stuDent: Student
        if pickerView  == pickerView1 {
            print(choices[row])
            stuDent = Student(hoursTaken: hourstaken!)
            model.student.append(stuDent)
        }
        subView.isHidden = true
        hoursTakenText.text = choices[row]
        print("hello")
    }
    override func viewDidLoad() {
        //pickerViewBack.isHidden = true
        super.viewDidLoad()        
        subView.isHidden = true 
    }
}

3 个答案:

答案 0 :(得分:0)

您在numberOfRowsInComponent的实施中选择了两个选择器视图,但在viewForRow的实现中却没有。您的viewForRow始终引用choices[row]并设置标签的文字。因此,两个选择器视图中都会显示choices

答案 1 :(得分:0)

您没有检查UIPickerViewviewForRow设置行的func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView { let label = (view as? UILabel) ?? UILabel() label.font = UIFont(name: "Verdana", size: 15) label.textAlignment = .center if pickerView == pickerView1 { label.text = choices[row] } else { label.text = choices2[row] } return label } ,因此您为两者设置了相同的数据。

shuffle

答案 2 :(得分:0)

您可以使用标签来执行此操作

var pickerTags: Int = -1

func pickersCon(){
 self.pickerView1.tag = 1
 self.pickerView1.delegate = self
 self.pickerView2.tag = 2
 self.pickerView2.delegate = self
}

在pickerView方法中,您可以执行以下操作 例如

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    if pickerView.tag == 1 {
        return choices[row]}
    else if pickerView.tag == 2 {
        return choices2[row]
    }
}