私は1つのdatepickerを持っています。私は、ユーザに "Von"テキストフィールド(明日午前9時に開始する)とその後にテキストフィールド "bis"(1.5時間後に始まる)の間で日付と時刻を選択させたいと思っています。2つのテキストフィールドを持つUIDatePicker(from/until date)は、 "until"テキストフィールドで正しい日付を設定できません。
すべて正常に動作します。しかし、私は "von"テキストフィールドをクリックします。私のdatepicker dintは自分自身に正しい時間を設定しました。ここで
は、いくつかのスクリーンショットは以下のとおりです。 from textfield、until textfield
のviewDidLoad:ここ
NSDate *today = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[gregorian setLocale:[NSLocale currentLocale]];
NSDateComponents *nowComponents = [gregorian components:NSYearCalendarUnit|NSMonthCalendarUnit| NSWeekdayCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:today];
[nowComponents setWeek: [nowComponents week]];
[nowComponents setDay:[nowComponents day]+1];
[nowComponents setHour:9];
[nowComponents setMinute:0];
[nowComponents setSecond:0];
today = [gregorian dateFromComponents:nowComponents];
NSDate *tomorrowat9am = [gregorian dateFromComponents:nowComponents];
//DatePicker wird erstellt
self.thePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 152, 325, 300)];
self.thePicker.datePickerMode = UIDatePickerModeDateAndTime;
[thePicker addTarget:self action:@selector(dateChanged) forControlEvents:UIControlEventValueChanged];
//starts from tomorrow at 9 am
NSDate *pickereinstelldate= tomorrowat9am;
[self.thePicker setDate:pickereinstelldate];
//set the right 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 *neuesdatum = [beginningOfWeek dateByAddingTimeInterval:3600*1.5]; NSString *neuesd = [dateFormatter stringFromDate:neuesdatum];
//set the textfield with the new date
tfVon.text=datum;
tfBis.text=neuesd;
[self.view addSubview:thePicker];
self.thePicker.hidden=YES;
tfVon.inputView=thePicker;
tfBis.inputView=thePicker;
}
はアクションです:私は何に私のdateboolを設定していないのはここ
-(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 "Von" textfield
if (dateBOOL==YES)
{
tfVon.text=datum;
tfBis.text=neuesd;
}
//when i click on "bis" textfield
if (dateBOOL==NO) {
tfBis.text=neuesd;
}
}
は次のとおりです。
-(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;
}
不明な質問があります。 – Hiren
いつ日付ピッカーを表示しますか?そして 'dateBOOL'をどこに' 'いいえ 'に設定しますか?いくつかのコードを投稿できますか? – sch
textxフィールドが選択されたときに私のdatepickerを表示します(自分のコードを更新しました)。日付帳はtextfieldにある必要があります。 –