I need a slider for displaying real time in slider-label from 12-00 AM to 11-45 PM with step size 15 minutes. But I just have no idea how to do it. Сan anybody give some suggestions to me?
4 Answers
If you just need a time chooser with slider, use the following code in the sliderChanged: handler:
- (IBAction)onSliderChange:(id)sender {
UISlider *slider = (UISlider *)sender;
NSUInteger numberOfSlots = 24*4 - 1; //total number of 15mins slots
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"h-mm a"];
NSDate *zeroDate = [dateFormatter dateFromString:@"12-00 AM"];
NSUInteger actualSlot = roundf(numberOfSlots*slider.value);
NSTimeInterval slotInterval = actualSlot * 15 * 60;
NSDate *slotDate = [NSDate dateWithTimeInterval:slotInterval sinceDate:zeroDate];
[dateFormatter setDateFormat:@"h-mm a"];
self.timeLabel.text = [dateFormatter stringFromDate:slotDate];
}

- 6,313
- 1
- 28
- 27
-
Hello @Denis, how to do this for 24-hour format from device settings, I mean if user selects 24-hour format, this logic doesnt work. – Ranjit Apr 03 '14 at 13:21
-
Well, of course this will not work automagically, because it's hard-coded in date format to include AM/PM symbol. However, you can slightly expand the logic from the example, and support 2 different formats depending on if your system uses AM/PM or 24h format. To detect that you can use for example [this approach](http://stackoverflow.com/questions/7448360/detect-if-time-format-is-in-12hr-or-24hr-format) – Denis Apr 03 '14 at 13:31
-
@Denis, is there no in other way, in which it can be performed – Ranjit Apr 03 '14 at 13:40
-
sorry, I don't see, what do you mean by 'other way' – Denis Apr 03 '14 at 15:54
-
Hello @Denis, whenever I move silder and close the View and reopen again, I want the slider position to be at the same place which I moved previously, How can we do that – Ranjit Apr 04 '14 at 06:59
-
1just store/restore the slider.value property and call onSliderChange: manually – Denis Apr 04 '14 at 07:50
-
if I check slider.value in viewDidload, I get a weird value, I dont get time, because, value property of slider is float and we are using here time. – Ranjit Apr 04 '14 at 08:00
-
Hello @Denis, I am facing one more problem with this code, as its hard coded, if I change region to Japan, this logic fails. How can we solve this. – Ranjit Apr 04 '14 at 09:30
-
Hello @Denis, when it is 24-hr format I am using this NSDate *zeroDate = [dateFormatter dateFromString:@"00:00"]; Is it right? – Ranjit Apr 04 '14 at 10:29
-
@Denis Please suggest http://stackoverflow.com/questions/33873530/i-want-to-switch-time-scrubber-to-do-15-min-intervals – Himanshu Parashar Nov 24 '15 at 07:10
Create a slider, and in the view controller, have the slider change the label as the value on the slider changed. To set the range of values, change the range in the Storyboard under the properties (you can set the change interval to steps of 15 there too). You may want to give it the interval in minutes or something similar and then just convert that to time :)
- (IBAction)sliderChanged:(UISlider *)sender

- 788
- 12
- 33
-
I need value of time that user choice by swiping slider and this time should be in label for user. – stalk Dec 26 '11 at 15:27
I'm adding another answer to add code. Yes, you can get the value from the slider and then set the text of the label after converting the value to a time. Like so:
// Add 0.5 to slider value and truncate it to an integer by type casting it as integer
int sliderIntValue = (int)([sender value] + 0.5f);
//do some conversion and set the label to the value
self.sliderLabel.text = newLabelText;

- 788
- 12
- 33
You may use NSTimer to call a method every 15 minutes and update the slider/label with the current time.
You may want to take a look at NSDateFormatter, it may be useful to obtain proper string representation for your date.

- 10,768
- 9
- 50
- 79