2014-04-04 40 views
1

我對凝固時間的工作,每當一個滑塊移動,我跟着這個鏈接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小時,它的工作原理,我不明白我在這裏做了什麼錯誤。

+0

「24小時格式無法正常工作」是什麼意思?怎麼了? – user3071962

+0

當我移動滑塊時,時間不會改變。 – Ranjit

+0

現在我假設'[dateFormatter dateWithString:]'將零返回到zeroDate,因爲它無法分析傳遞的字符串。所有後續的操作zeroDate也將返回零。這應該很容易在調試器中驗證,對吧? – user3071962

回答

2

我認爲你過於複雜,你在做什麼。您不需要使用基於用戶正在使用的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

+0

謝謝,這正是我想要達到的目標..我有一個懷疑,但他們爲什麼不需要這樣做[dateFormatter setDateFormat:@「h:mm a」]; – Ranjit

+1

由於'[self.formatter setLocale:[NSLocale currentLocale]];'它使用用戶的語言環境來決定是否使用12或24小時格式。 – Abizern

+0

@Abizern請建議http://stackoverflow.com/questions/33873530/i-want-to-switch-time-scrubber-to-do-15-min-intervals –