Search code examples
iosobjective-cuitextfielduidatepicker

Objective C: Begin date and end date UIDatePicker


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];
}

Solution

  • Add this after [self.beginTijdTextField setInputView:beginTijdPicker]; :

    [self.beginTijdTextField addTarget:self action:@selector(beginTijdDidChange) forControlEvents:UIControlEventEditingChanged];
    

    and add this method

    -(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;
    }