Search code examples
iosswifttimerdatepicker

Date picker with minutes and seconds


I'm new to Swift and fairly new to programming in general. I'm making a basic countdown timer app and want to use a date picker to select the duration of the countdown. I have a label which is a concatenation of a timeMin variable and a timeSec variable, seperated by a ":" string.

When the date picker's mode is set to countdown timer, it can only do hours and minutes. Is there any way I could change this to minutes and seconds? The text displayed on the date picker should be minutes and seconds too, rather than hours and minutes.

So if my label starts off as 02:00, using the date picker and selecting 4 in minutes and 35 in seconds should simply result in the label changing to 04:35.


Solution

  • This is not possible with UIDatePicker, If you want to implement this, then you have to create custom datepicker by using UIPickerView. You have to take 2 components in UIPickerView, one for minutes and one for seconds. And supply the values(with appropriate interval) from titleForRow: ... method of UIPickerViewDatasource.