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

前端之家收集整理的这篇文章主要介绍了swift – 如何使用UIPickerView设置第二个组件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我发现了一些类似的问题,但没有一个解决方案符合我的情况,而且大部分都是客观的,我不知道.

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

如何使用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,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()

    }

这是结果:

原文链接:https://www.f2er.com/swift/318967.html

猜你在找的Swift相关文章