Home>

I want to set Timer in UIPickerView.

Applicable source code
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
    @IBOutlet weak var testPickerView: UIPickerView!
// @IBOutlet weak var testLabel: UILabel!
    var timer: Timer!
    var count: Int = 0
    // hour minute second data
    let dataList = [[Int] (0 ... 24), [Int] (0 ... 60), [Int] (0 ... 60)]
    override func viewDidLoad () {
        super.viewDidLoad ()
        // Do any additional setup after loading the view, typically from a nib.
        // add "time" label
        let hStr = UILabel ()
        hStr.text = "Time"
        hStr.sizeToFit ()
        hStr.frame = CGRect (x: testPickerView.bounds.width/4-hStr.bounds.width/2,
                            y: testPickerView.bounds.height/2-(hStr.bounds.height/2),
                            width: hStr.bounds.width, height: hStr.bounds.height)
        testPickerView.addSubview (hStr)
        // add "minutes" label
        let mStr = UILabel ()
        mStr.text = "minutes"
        mStr.sizeToFit ()
        mStr.frame = CGRect (x: testPickerView.bounds.width/2-mStr.bounds.width/2,
                            y: testPickerView.bounds.height/2-(mStr.bounds.height/2),
                            width: mStr.bounds.width, height: mStr.bounds.height)
        testPickerView.addSubview (mStr)

        // add "second" label
        let sStr = UILabel ()
        sStr.text = "seconds"
        sStr.sizeToFit ()
        sStr.frame = CGRect (x: testPickerView.bounds.width * 3/4-sStr.bounds.width/2,
                            y: testPickerView.bounds.height/2-(sStr.bounds.height/2),
                            width: sStr.bounds.width, height: sStr.bounds.height)
        testPickerView.addSubview (sStr)
    }
    override func didReceiveMemoryWarning () {
        super.didReceiveMemoryWarning ()
        // Dispose of any resources that can be recreated.
    }
    // Method that returns the number of components
    func numberOfComponents (in pickerView: UIPickerView)->Int {
        return dataList.count
    }

    // Method that returns the number of data contained in the component
    func pickerView (_ pickerView: UIPickerView, numberOfRowsInComponent component: Int)->Int {
        return dataList [component] .count
    }

    // method to return size
    func pickerView (_ pickerView: UIPickerView, widthForComponent component: Int)->CGFloat {
        return testPickerView.bounds.width * 1/4
    }

    // Method that returns data
    func pickerView (_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?)->UIView {
        let pickerLabel = UILabel ()
        pickerLabel.textAlignment = NSTextAlignment.left
        pickerLabel.text = String (dataList [component] [row])
        pickerLabel.backgroundColor = UIColor.red
        return pickerLabel
    }
}
Troubleshooting

UIPickerView options are not displayed when executed

Supplemental information (FW/tool version etc.)

swift4.1
ios11.4

  • Answer # 1

    Did you set the picker view delegate?

    Is it set in ViewDidLoad () with code?

    // Set in ViewDidLoad
    override func viewDidLoad () {
            super.viewDidLoad ()
            // various things omitted
            // delegate settings
            testPickerView.delegate = self
            testPickerView.dataSource = self
    }

    What happens if I set it from the Storyboard ↓
    http://hajihaji-lemon.com/smartphone/swift/uipickerview/
    The part of `` ▶ place the part ''

  • Answer # 2

    pickerView (_: viewForRow: forComponent: reusing:)rather than usingpickerView (_: titleForRow: forComponent:)is easier to implement.