Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how have hour:minutes:seconds in Date Picker swift

Im programming an application that have timer which user can set the time to the timer(on picker view) and it will be count down.so in date picker i selected countdown mode.but i don't know how work with that any body can help me? I'm so new in swift. in fact i want user set time to date picker and then when click on start in begin to count down but date picker has only hour and minute and don't has second, what should i do? i can set time of picker view to a label but picker view hasn't second

let timeLeft = datePicker.date
    let formatter = DateFormatter()
    formatter.dateFormat = "HH:mm:ss"
    let formatteddate = formatter.string(from: timeLeft)
    countinLabel.text = formatteddate
like image 892
nino Avatar asked Dec 16 '17 09:12

nino


2 Answers

You should try with UIPickerView for hours : minutes : seconds format.
Because: The only supported time format supported by UIDatePicker is hours : minutes.

class ViewController: UIViewController {

    @IBOutlet weak var toolbar: UIToolbar!

    @IBOutlet weak var picker: UIPickerView!

    var hour:Int = 0
    var minutes:Int = 0
    var seconds:Int = 0

}

DataSource and Delegate as below:

extension ViewController:UIPickerViewDelegate,UIPickerViewDataSource {
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 3
    }
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        switch component {
        case 0:
            return 25
        case 1,2:
            return 60

        default:
            return 0
        }
    }

    func pickerView(_ pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {
        return pickerView.frame.size.width/3
    }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        switch component {
        case 0:
            return "\(row) Hour"
        case 1:
            return "\(row) Minute"
        case 2:
            return "\(row) Second"
        default:
            return ""
        }
    }
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        switch component {
        case 0:
            hour = row
        case 1:
            minutes = row
        case 2:
            seconds = row
        default:
            break;
        }
    }
}
like image 163
Jeetendra Kumar Avatar answered Sep 25 '22 00:09

Jeetendra Kumar


The correct code from previous answer, which works swift 5.1 and xcode 11.1:

class TimerSelectViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        pickerView.delegate = self
    }

    @IBOutlet var pickerView: UIPickerView!

    var hour: Int = 0
    var minutes: Int = 0
    var seconds: Int = 0

}

     extension TimerSelectViewController: UIPickerViewDelegate, UIPickerViewDataSource {

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

         func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
             switch component {
             case 0:
                 return 25
             case 1, 2:
                 return 60
             default:
                 return 0
             }
         }

         func pickerView(_ pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {
             return pickerView.frame.size.width/3
         }

         func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
             switch component {
             case 0:
                 return "\(row) Hour"
             case 1:
                 return "\(row) Minute"
             case 2:
                 return "\(row) Second"
             default:
                 return ""
             }
         }
         func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
             switch component {
             case 0:
                 hour = row
             case 1:
                 minutes = row
             case 2:
                 seconds = row
             default:
                 break;
             }
         }
     }
like image 43
ucelme Avatar answered Sep 24 '22 00:09

ucelme