在UISlider上设置时间更改

时间:2014-04-04 11:24:44

标签: ios nsdate nsdateformatter uislider

我正在设置滑块移动时的时间,我按照这个链接Slider with real time in Label并让大部分工作正常工作,这是代码,我目前正在处理

//此代码用于检查系统设置是24小时格式还是12小时格式

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[NSLocale currentLocale]];
[formatter setDateStyle:NSDateFormatterNoStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
NSString *dateString = [formatter stringFromDate:[NSDate date]];
NSRange amRange = [dateString rangeOfString:[formatter AMSymbol]];
NSRange pmRange = [dateString rangeOfString:[formatter PMSymbol]];
m_is24h = (amRange.location == NSNotFound && pmRange.location == NSNotFound);

这是滑块代码

UISlider *slider = (UISlider *)sender;

NSUInteger numberOfSlots = 24*2 - 1; //total number of 30mins slots

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"h:mm a"];


NSDate *zeroDate;
NSString *amString = [[formatter AMSymbol]uppercaseString];
NSString *timeString = [[[NSString stringWithFormat:@"12:00"]stringByAppendingString:@" "]stringByAppendingString:amString];


if(m_is24h)
{
     zeroDate = [dateFormatter dateFromString:@"00:00"];
}
else
{
     zeroDate = [dateFormatter dateFromString:timeString];
}


NSUInteger actualSlot = roundf(numberOfSlots*slider.value);
NSTimeInterval slotInterval = actualSlot * 30 * 60;

NSDate *slotDate = [NSDate dateWithTimeInterval:slotInterval sinceDate:zeroDate];
[dateFormatter setDateFormat:@"h:mm a"];

m_timeLabel.text = [[dateFormatter stringFromDate:slotDate]uppercaseString];

我在印度地区测试了这个代码,24小时和12小时格式的一切都运行正常。现在我将区域更改为日本或欧洲或任何其他地区时,24小时格式在移动滑块时不起作用,但是如果我将格式从设置更改为12小时,它可以工作,我不明白我在这里做了什么错误。

1 个答案:

答案 0 :(得分:3)

我认为你的工作过于复杂。您不需要根据用户使用的12或24小时格式为字符串创建日期,您只需使用NSDate并让格式化程序正确显示日期。

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property (weak, nonatomic) IBOutlet UISlider *slider;
@property (strong, nonatomic) NSDateFormatter *formatter;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.formatter = [NSDateFormatter new];
    [self.formatter setLocale:[NSLocale currentLocale]];
    [self.formatter setDateStyle:NSDateFormatterNoStyle];
    [self.formatter setTimeStyle:NSDateFormatterShortStyle];

    // Lazy way to set up the initial time
    [self sliderMoved:self.slider];

}

#pragma mark - Actions

- (IBAction)sliderMoved:(UISlider *)sender {
    NSUInteger slot = sender.value;
    NSDate *slotDate = [self timeFromSlot:slot];
    self.timeLabel.text = [self.formatter stringFromDate:slotDate];
}


#pragma mark - Private methods

/**
 Converts a slot integer to a valid time in 30 minute increments

 @param slot The slot number

 @return An NSDate for the time representing the slot

 @warning slot should be between 0 and 47
 */

- (NSDate *)timeFromSlot:(NSUInteger)slot{
    if ((slot > 47)) {
        return nil;
    }

    NSDateComponents *components = [NSDateComponents new];
    [components setMinute:30 * slot];

    return [[NSCalendar currentCalendar] dateFromComponents:components];
}

@end

这是完整的视图控制器实现,可以执行您想要的操作。如果您不想自己创建测试项目,则完整项目为available here