2010-12-07 64 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