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.