我有一个显示小时的按钮 - 当视图加载它时设置为当前时间。 然后,当我加载日期选择器子视图时,它始终设置为比它早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];
在我们选择时区之前,日期按钮被设置为比实际时间早两小时,并且日期选择器被设置为实时,仍然是同样的问题。 – 2010-12-07 09:56:24