私はDatePickerを持っています。ユーザーに「from」テキストフィールド(明日の午前9時に開始)で日付と時刻を選択させ、次にテキストフィールド「until」(1.5時間後に開始)を選択させます。
例えば。"From" TextField: 25.04.2012 09:00、"Until" TextField: 25.04.2012 10:30
私がアップロードした写真を見てください。
これまでのところすべて正常に動作していますが、「まで」テキストフィールドをクリックすると、Datepicker が正しい日付と時刻を設定しませんでした。彼は 25.04.2012 10:30 の代わりに 25.04.2012 09:00 を設定します
これまでの私のコード:
ViewDidLoad:
//Create DatePicker
self.thePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 152, 325, 300)];
self.thePicker.datePickerMode = UIDatePickerModeDateAndTime;
[thePicker addTarget:self action:@selector(dateChanged) forControlEvents:UIControlEventValueChanged];
//Set DatePicker to Tomorrow 9:00 o'clock
NSDate *today = [NSDate date];
NSCalendar *german = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[german setLocale:[NSLocale currentLocale]];
NSDateComponents *nowComponents = [german components:NSYearCalendarUnit | NSWeekCalendarUnit | NSHourCalendarUnit | NSDayCalendarUnit| NSMonthCalendarUnit| NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:today];
[nowComponents setDay:[nowComponents day] +1];
[nowComponents setWeek: [nowComponents week]];
[nowComponents setHour:9];
[nowComponents setMinute:0];
[nowComponents setSecond:0];
NSDate *beginningOfWeek = [german dateFromComponents:nowComponents];
NSDate *pickereinstelldate= beginningOfWeek;
[self.thePicker setDate:pickereinstelldate];
//Set DatePicker to correct Format
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd.MM.yyyy HH:mm"];
NSString *datum =[dateFormatter stringFromDate:self.thePicker.date];
//set the date at textfield "bis" to 1.5 hours later
NSDate *morgenDate = [beginningOfWeek dateByAddingTimeInterval:3600*1.5];
NSString *newdate = [dateFormatter stringFromDate:morgenDate];
//Set the until TextField with the newdate
tfVon.text=datum;
tfBis.text=newdate;
[self.view addSubview:thePicker];
self.thePicker.hidden=YES;
tfVon.inputView=thePicker;
tfBis.inputView=thePicker;
}
アクション機能:
-(IBAction)dateChanged
{
self.date = [thePicker date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd.MM.yyyy HH:mm"];
NSString *datum =[dateFormatter stringFromDate:self.date];
NSDate *neuesdatum = [date dateByAddingTimeInterval:3600*1.5];
NSString *neuesd = [dateFormatter stringFromDate:neuesdatum];
//when i click on "From" textfield
if (dateBOOL==YES)
{
tfVon.text=datum;
tfBis.text=neuesd;
}
//when i click on "Until" textfield
if (dateBOOL==NO) {
tfBis.text=neuesd;
}
}
ここで、datebool を no に設定します。
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if (textField==tfVon)
{
dateBOOL=YES;
self.thePicker.hidden=NO;
return YES;
}
if (textField==tfBis)
{
dateBOOL=NO;
self.thePicker.hidden=NO;
return YES;
}
}
return YES;
}
"Until" TextField の DatePicker を正しい時刻に設定する方法はありますか?