如何使用 UIPickerView 设置第二个组件

How do I setup a second component with a UIPickerView

我发现了一些与此类似的问题,但是 none 的解决方案符合我的情况,而且大多数在 objective-c 中,我不知道。

我正在尝试创建一个带有分钟和秒的计时器,但我不知道如何连接我的第二个组件。

如何使用 UIPickerView 设置第二个组件?

这是我目前拥有的:

TimeViewController.swift

class TimeViewController: UIViewController, UIPickerViewDelegate {

    @IBOutlet weak var timeSegueLabel: UILabel!

    let minutes = Array(0...9)
    let seconds = Array(0...59)

    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {

        return 2
    }

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

        return minutes.count

    }

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

        return String(minutes[row])

    }

    override func viewDidLoad() {
        super.viewDidLoad()

    }
}

感谢@Bluehound 为我指明了正确的方向。我不得不查看 GitHub 上的一些 objective-c 以找出确切的操作方法,这就是我正在寻找的答案:

TimeViewController:

class TimeViewController: UIViewController, UIPickerViewDelegate {

    @IBOutlet weak var timeSegueLabel: UILabel!

    let minutes = Array(0...9)
    let seconds = Array(0...59)

    var recievedString: String = ""

    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {

        return 2

    }

    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        //row = [repeatPickerView selectedRowInComponent:0];
        var row = pickerView.selectedRowInComponent(0)
        println("this is the pickerView\(row)")

        if component == 0 {
            return minutes.count
        }

        else {
            return seconds.count
        }

    }

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

        if component == 0 {
        return String(minutes[row])
        } else {

        return String(seconds[row])
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        timeSegueLabel.text = recievedString

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

    }

这是结果:

可以在两个标签中显示 2 个组件的 Picker

swift 4 xcode 9

class ViewController: UIViewController, UIPickerViewDelegate,  UIPickerViewDataSource {

    @IBOutlet weak var lbl1: UILabel!
    @IBOutlet weak var lbl2: UILabel!
    @IBOutlet weak var picker1: UIPickerView!

    var country = ["UK","US","JAPAN","UAE","CHINA","CANADA","EUROPE","IRAN","INDIA","ITALY" ]
    var capital = [ "LONDON","WASHIGTON DC","TOKYO","ABU DHABI","BEIJING","OTTAWA","BRUSSELS","TEHRAN","NEW DELHI","ROME" ]
    var val = 0

    override func viewDidLoad() {
        super.viewDidLoad()
    }

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

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

        if component == 0 {
            return country.count
        } else {
            return capital.count
        }
    }

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

        if component == 0 {
            return String(country[row])
        } else {
            return String(capital[row])
        }
    }

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

        if component == 0 {
            lbl1.text = country[row]
        } else {
            lbl2.text = capital[row]
        }
    }
}