我有一個日期選擇器。我想讓用戶在「Von」文本字段(明天上午9點開始)中選擇日期和時間,然後直到textfield「bis」(1.5小時後開始)。UIDatePicker與2個文本框(從/直到日期)不能設置正確的日期在「直到」文本域
一切工作正常。但是當我點擊「馮」文本框。我的日期選擇器爲自己設定了正確的時間。
下面是一些截圖: 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;
}
這裏是動作:
-(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;
}
}
這裏是我我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;
}
不清楚的問題。請說清楚 – Hiren 2012-03-23 10:11:47
您什麼時候顯示日期選擇器?你在哪裏設置'dateBOOL'爲'NO'?你可以發佈一些代碼嗎? – sch 2012-03-23 10:34:16
當我選擇textxfield(我更新了我的代碼)時,顯示我的日期選擇器。在textfield中的記事本應該被限制爲 – 2012-03-23 12:52:10