我有一個顯示小時的按鈕 - 當視圖加載它時設置爲當前時間。 然後,當我加載日期選擇器子視圖時,它始終設置爲比它早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