实时标签中的滑块

我需要一个滑块来实时显示12-00 AM到11-45 PM的滑块标签,步长为15分钟。 但我不知道如何去做。 有人给我一些build议吗?

如果您只需要带滑块的时间select器,请在sliderChanged:处理程序中使用以下代码:

- (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]; } 

创build一个滑块,并在视图控制器中,使滑块更改标签,因为滑块上的值已更改。 要设置值的范围,请在属性下的故事板中更改范围(您也可以将更改间隔设置为15步)。 你可能要给它间隔几分钟或类似的东西,然后把它转换为时间:)

 - (IBAction)sliderChanged:(UISlider *)sender 

我添加了另一个添加代码的答案。 是的,您可以从滑块中获取值,然后在将值转换为时间后设置标签的文本。 像这样:

 // 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; 

您可以使用NSTimer每15分钟调用一次方法,并使用当前时间更新滑块/标签。

你可能想看一下NSDateFormatter ,为你的date获取正确的string表示可能是有用的。