Reputation: 4646
I am working on setting time whenever a slider moves, I followed this link Slider with real time in Label and got most of the stuff working, here is the code, which I am presently working on
//This code is to check whether the system settings is 24hr format or 12hr format
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);
Here is the slider code
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];
I tested this code with India region and everything works fine both for 24hr and 12hr format.Now when I change the region to Japan or Europe or any other region, the 24 hour format will not work when I move the slider, but If I change the format to 12hr from settings, it works, I am not understanding what mistake I am doing here.
Upvotes: 2
Views: 1640
Reputation: 150675
I think you are overcomplicating what you are doing. You don't need to create a date for the slot with a string based on the 12 or 24 hour format that the user is using, you can just use an NSDate and let the formatter display the date appropriately.
@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
This is the complete view controller implementation that does what you seem to want. The full project is available here if you don't want to create a test project yourself.
Upvotes: 3