I have two UIDatePickers
and two UITextField
.
One UIDatePicker
and textField
show the begin date + time.
The second UIDatePicker
and textField
shows the end date +time.
The problem i'm facing is that i need to connect the two DatePickers
so that the end date cannot exceed the begin date + time. Also that there cannot be more than 24hours between the begin and end date + time. What do i need to do in order to achieve this? How do i connect the two UIDatePicker
, and prevent the dates + time from exceeding each other if this is possible?
My UIDatePicker
pops up when the textField
is being clicked (instead of the keyboard).
I'm still learning, so please bear with me. I'm trying my best to understand.
Before anyone tells me this post already exists, it doesn't, not the way i want my UIDatePicker
to work. (Please compare my code to theirs, its different)
The code i have for the UIDatePicker
is :
.h file:
@
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 file:
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];
}
Enddate (same as begindate but different names):
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];
}
Add this after
[self.beginTijdTextField setInputView:beginTijdPicker];
:and add this method
Beside the already solved problem the UI seems a bit convoluted. If it is known that one time has to be 0-24 hours after the other why not just let the user pick that, i.e. start date-time and a 0-24 hour time interval as pickers and then calculate the end date as start + interval? Seems easier to use to me.
Yeh! you can set Maximum date and minimum date to the Second UIDatePicker.