Только для времени UIDatePicker отображает на 2 часа больше текущего времени - PullRequest
0 голосов
/ 07 декабря 2010

У меня есть кнопка, которая отображает часы - при загрузке представления устанавливается текущее время. Затем, когда я загружаю подпредставление выбора даты, оно всегда устанавливается на 2 часа раньше. что-то вроде того: Кнопка даты: 10:31 Выбор даты: 12:31 после изменения часа в средстве выбора даты на: 13:31 кнопка даты изменится на: 11: 31.

код:

-(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];

Ответы [ 2 ]

0 голосов
/ 07 декабря 2010

решаемая. Проблема была с stringFromTime - [описание даты] возвращает неправильное значение.

0 голосов
/ 07 декабря 2010

Хотя я не разработал для iPhone или для target-C, так что я не на 100% по синтаксису и тому подобное, но мне кажется, что вы в какой-то момент переводите время ввода в GMT (или UTC) а с другой ты нет.Это может объяснить 2-х часовое расхождение, поскольку выбранный вами часовой пояс Израиля / Иерусалима составляет UTC + 2 (+3 IST).Я бы постарался не «сдвинуть» время и посмотреть, поможет ли это.Если да, то, по крайней мере, вы нашли проблему, я бы сказал.

...