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

时间:2015-04-14 01:43:09

标签: swift uipickerview

我发现了一些类似的问题,但没有一个解决方案符合我的情况,而且大多数都是客观的,我不知道。

我正在尝试创建一个具有分钟和秒的计时器,但我无法弄清楚如何连接我的第二个组件。

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

这是我到目前为止所做的:

enter image description here

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()

    }
}

2 个答案:

答案 0 :(得分:18)

感谢@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()

    }

结果如下:

enter image description here

答案 1 :(得分:0)

可以在两个标签中显示两个组件的情况下执行Picker

交换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]
        }
    }
}