2010-12-07 63 views
0

我有一個顯示小時的按鈕 - 當視圖加載它時設置爲當前時間。 然後,當我加載日期選擇器子視圖時,它始終設置爲比它早2小時。 類似的東西: 日期按鈕:10:31日期選擇器:12:31 將日期選擇器中的小時更改爲:13:31日期按鈕更改爲:11:31。僅限時間UIDatePicker顯示比當前時間提前2小時

代碼:

-(IBAction) timeClicked 
{ 
    [[NSBundle mainBundle] loadNibNamed:@"timePicker" owner:self options:nil]; 
    //timeView = [[UIView alloc]initWithNibName:@"timePicker" bundle:nil]; 
    [timeView setFrame:CGRectMake(0, 480, 320, 431)]; 
    NSLog(@"time clicked date: %@", select); 
    NSDate* sourceDate = [NSDate date]; 

    NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithName:@"Jerusalem"]; 
    NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone]; 

    NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate]; 
    NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate]; 
    NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset; 

    NSDate* destinationDate = [[[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate] autorelease]; 


    //[timePick setTime:destinationDate animated:YES]; 
    timePick.date=destinationDate; 
    NSLog(@"befoer delegate"); 
    //[timeView setDelegate:self]; 
    [self.view addSubview:timeView]; 
    CGRect frame = timeView.frame; 
    frame.origin.y = 110; 
    [UIView beginAnimations:nil context:NULL]; 
    timeView.frame = frame; 
    [UIView commitAnimations]; 
    NSLog(@"after"); 

} 


-(IBAction) done 
{ 
    select = [timePick date]; 
    [UIView beginAnimations:@"RemoveDatePicker" context:NULL]; 
    [UIView setAnimationDidStopSelector:@selector(transitionDidStop:finished:context:)]; 
    CGRect rect = timeView.frame; 
    rect.origin.y = 460; 
    timeView.frame = rect; 
    [UIView commitAnimations]; 

} 

- (void)transitionDidStop:(NSString *)animationID finished:(BOOL)finished context:(void *)context 
{ 
    if ([animationID isEqualToString:@"RemoveDatePicker"]){ 
     [timeView removeFromSuperview]; 
     NSLog(@"RemoveDatePicker"); 
    } 

} 


-(IBAction) timeChanged 
{ 
    select =[timePick date]; 

    [timeButton setTitle:[Path stringFromTime:select] forState:UIControlStateNormal]; 


    //[timeButton setTitle:@"time" forState:UIControlStateNormal]; 
} 

+(NSString *) stringFromTime: (NSDate *) date 
{ 

    NSString * stringDate = [date description]; 
    stringDate = [[stringDate substringFromIndex:11] substringToIndex:5]; 
    NSLog(@"[Path]stringTime: %@", stringDate); 
    return stringDate; 

} 

從視圖中做負載:

NSDate* sourceDate = [NSDate date]; 

    NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithName:@"Jerusalem"]; 
    NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone]; 

    NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate]; 
    NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate]; 
    NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset; 

    NSDate* destinationDate = [[[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate] autorelease]; 

    timeModeChange.selectedSegmentIndex=1; 
    select=[[NSDate alloc]initWithDate:destinationDate ]; 
     NSLog(@"date %@", select); 
    [timeButton setTitle:[Path stringFromTime:select] forState:UIControlStateNormal]; 

回答

0

已解決。 問題出在stringFromTime - [日期描述]返回錯誤的值。

0

雖然我還沒有爲iPhone或在Objective-C開發的,所以我不是100%的語法等,但在我看來,你在某一時刻將你的輸入時間翻譯成GMT(或UTC),而在另一時刻則不是。這可能會導致自您選擇的時區(即以色列/耶路撒冷時區)出現2小時差異爲UTC + 2(+3 IST)。我會盡量不「移動」時間,看看是否有幫助。如果確實如此,那麼你至少會發現問題,我會說。

+0

在我們選擇時區之前,日期按鈕被設置爲比實際時間早兩小時,並且日期選擇器被設置爲實時,仍然是同樣的問題。 – 2010-12-07 09:56:24