我有两个UIDatePickers
和两个UITextField
。UIDatePicker
和textField
分别显示开始日期和时间。
第二个UIDatePicker
和textField
显示结束日期+时间。
我面临的问题是我需要连接两个DatePickers
,以便结束日期不能超过开始日期+时间。另外,开始日期和结束日期+时间之间不能超过24小时。为了达到这个目的,我需要做什么?我如何连接两个UIDatePicker
,并在可能的情况下防止日期和时间彼此超出?
单击UIDatePicker
(而不是键盘)时,我的textField
会弹出。
我还在学习,所以请忍受。我正在尽力了解。
在没有人告诉我这篇文章已经存在之前,还不存在,不是我希望我的UIDatePicker
工作的方式。 (请比较我的代码与他们的代码,不同)
我为UIDatePicker
提供的代码是:
.h文件:
@
interface TaakToevoegen : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate, UITextViewDelegate>{
UIDatePicker *beginTijdPicker;
UIDatePicker *eindTijdPicker;
}
//TextField for both begindate and enddate.
@property (strong, nonatomic) IBOutlet UITextField *beginTijdTextField;
@property (strong, nonatomic) IBOutlet UITextField *eindTijdTextField;
.m文件:
beginTijdPicker=[[UIDatePicker alloc] init];
beginTijdPicker.datePickerMode=UIDatePickerModeDateAndTime;
//Sets time to 24hour notation instead of AM & PM
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"NL"];
[beginTijdPicker setLocale:locale];
[self.beginTijdTextField setInputView:beginTijdPicker];
UIToolbar *toolBar=[[UIToolbar alloc] initWithFrame: CGRectMake(0, 0, 320, 44)];
[toolBar setTintColor:[UIColor grayColor]];
UIBarButtonItem *doneBtn=[[UIBarButtonItem alloc] initWithTitle:@"Done"
style:UIBarButtonItemStylePlain
target:self
action:@selector(ShowSelectedDate1)];
UIBarButtonItem *space= [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
[toolBar setItems:[NSArray arrayWithObjects:space, doneBtn, nil]];
[self.beginTijdTextField setInputAccessoryView:toolBar];
-(void)ShowSelectedDate1{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"dd MMM HH:mm"];
self.beginTijdTextField.text=[NSString stringWithFormat:@"%@", [formatter stringFromDate:beginTijdPicker.date]];
[self.beginTijdTextField resignFirstResponder];
}
结束日期(与开始日期相同,但名称不同):
eindTijdPicker=[[UIDatePicker alloc] init];
eindTijdPicker.datePickerMode=UIDatePickerModeDateAndTime;
//Sets time to 24hour notation instead of AM & PM
NSLocale *locale1 = [[NSLocale alloc] initWithLocaleIdentifier:@"NL"];
[eindTijdPicker setLocale:locale1];
[self.eindTijdTextField setInputView:eindTijdPicker];
UIToolbar *toolBar1=[[UIToolbar alloc] initWithFrame: CGRectMake(0, 0, 320, 44)];
[toolBar1 setTintColor:[UIColor grayColor]];
UIBarButtonItem *doneBtn1=[[UIBarButtonItem alloc] initWithTitle:@"Done"
style:UIBarButtonItemStylePlain
target:self
action:@selector(ShowSelectedDate2)];
UIBarButtonItem *space1= [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
[toolBar1 setItems:[NSArray arrayWithObjects:space1, doneBtn1, nil]];
[self.eindTijdTextField setInputAccessoryView:toolBar1];
-(void)ShowSelectedDate2{
NSDateFormatter *formatter1 = [[NSDateFormatter alloc] init];
[formatter1 setDateFormat:@"dd MMM HH:mm"];
self.eindTijdTextField.text=[NSString stringWithFormat:@"%@", [formatter1 stringFromDate:eindTijdPicker.date]];
[self.eindTijdTextField resignFirstResponder];
}
最佳答案
在[self.beginTijdTextField setInputView:beginTijdPicker];
之后添加:
[self.beginTijdTextField addTarget:self action:@selector(beginTijdDidChange) forControlEvents:UIControlEventEditingChanged];
并添加此方法
-(void)beginTijdDidChange
{
int oneDay = 1;
NSDate *maxDateFromBeginig = [beginTijdPicker.date dateByAddingTimeInterval:60*60*24*oneDay]; //24h + beginDate
NSDate *minDateFromBeginig = [NSDate dateWithTimeInterval:1.0 sinceDate:beginTijdPicker.date]; //1 sec + beginDate
eindTijdPicker.maximumDate = maxDateFromBeginig;
eindTijdPicker.minimumDate = minDateFromBeginig;
}
关于ios - objective-c :开始日期和结束日期UIDatePicker,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33940942/