Handling Datepicker and Timepicker in iOS Textfields for Advanced User Interfaces

Handling Datepicker and Timepicker in iOS Textfields

In this article, we will explore how to handle datepicker and timepicker in iOS textfields. We will discuss the delegate method that can be used to show pickers when a textfield is tapped.

Understanding the Problem

The problem at hand involves two textfields on an iOS screen. When the first textfield is tapped, a datepicker should appear. Similarly, when the second textfield is tapped, a timepicker should appear. However, the current implementation uses the same datepicker for both modes, which is not ideal.

Using the textFieldShouldBeginEditing Delegate Method

To handle this scenario, we need to use the textFieldShouldBeginEditing delegate method. This method is called when a textfield is about to begin editing, i.e., when it’s tapped.

The textFieldShouldBeginEditing method returns a boolean value indicating whether the editing should begin or not.

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    if (textField == self.datepictxt) {
        // Show date picker
        [self showDatePicker];
    } else if (textField == self.Timepicker) {
        // Show time picker
        [self showTimePicker];
    }
    return YES;
}

In the above code, we’re checking the textField parameter to determine which textfield has been tapped. If it’s the first textfield (self.datepictxt), we call a method called showDatePicker. Otherwise, if it’s the second textfield (self.Timepicker), we call a method called showTimePicker.

Showing Date Picker

To show the date picker, we can use a separate method. In this example, let’s assume that datepicker is an instance variable that will hold the datepicker object.

- (void)showDatePicker {
    self.datepictxt.delegate = self;
    
    UITextField *textField = (UITextField *)[self.view viewWithTag:1];
    
    // Create a new datepicker if one doesn't exist for this textfield
    if (!textField.tag) {
        textField.tag = 1;
        
        UIDatePicker *datepicker = [[UIDatePicker alloc] init];
        datepicker.datePickerMode = UIDatePickerModeDate;
        [self.datepictxt setInputView:datepicker];
        
        UIToolbar *toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
        [toolbar setTintColor:[UIColor grayColor]];
        UIBarButtonItem *donebtn = [[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(showSelectionDate:)];
        UIBarButtonItem *space = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
        [toolbar setItems:[NSArray arrayWithObjects:space, donebtn, nil]];
        [self.datepictxt setInputAccessoryView:toolbar];

    } else {
        // Update the existing datepicker
        UIDatePicker *datepicker = (UIDatePicker *)[[self.view viewWithTag:1]];
        datepicker.datePickerMode = UIDatePickerModeDate;
        [self.datepictxt setInputView:datepicker];
    }
}

In this code, we’re creating a new UIDatePicker instance and assigning it to the datepicker variable. We’re also setting up the toolbar with a “Done” button.

Showing Time Picker

Similarly, let’s assume that Timepicker is an instance variable that will hold the timepicker object.

- (void)showTimePicker {
    self.Timepicker.delegate = self;
    
    UITextField *textField = (UITextField *)[self.view viewWithTag:2];
    
    // Create a new timepicker if one doesn't exist for this textfield
    if (!textField.tag) {
        textField.tag = 2;
        
        UIDatePicker *timepicker = [[UIDatePicker alloc] init];
        timepicker.datePickerMode = UIDatePickerModeTime;
        [self.Timepicker setInputView:timepicker];
        
        UIToolbar *toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
        [toolbar setTintColor:[UIColor grayColor]];
        UIBarButtonItem *donebtn = [[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(showSelectionTime:)];
        UIBarButtonItem *space = [[UIBarButtonItem allocinitWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
        [toolbar setItems:[NSArray arrayWithObjects:space, donebtn, nil]];
        [self.Timepicker setInputAccessoryView:toolbar];

    } else {
        // Update the existing timepicker
        UIDatePicker *timepicker = (UIDatePicker *)[[self.view viewWithTag:2]];
        timepicker.datePickerMode = UIDatePickerModeTime;
        [self.Timepicker setInputView:timepicker];
    }
}

In this code, we’re creating a new UIDatePicker instance and assigning it to the Timepicker variable. We’re also setting up the toolbar with a “Done” button.

Handling Date Selection

To handle date selection, we need to implement the datePicker:didSelectDate:timeZone:forView: delegate method of UIDatePickerDelegate.

- (void) datePicker:(UIDatePicker *)picker didSelectDate:(NSDate *)date timeZone:(NSTimeZone *)timeZone forView:(UIView *)view {
    // Handle date selection here
    
    // For now, let's just print the selected date
    NSLog(@"Selected Date: %@", date);
}

Similarly, we need to implement the timePickerDidChange: delegate method of UIDatePickerDelegate to handle time selection.

- (void) timePickerDidChange:(UIDatePicker *)picker {
    // Handle time selection here
    
    // For now, let's just print the selected time
    NSLog(@"Selected Time: %@", [NSDate dateWithTimeIntervalSinceNow:[picker valueForDateComponent:UIDatePickerHourComponents]]];
}

Conclusion

In this article, we’ve explored how to handle datepicker and timepicker in iOS textfields. We’ve discussed the textFieldShouldBeginEditing delegate method that can be used to show pickers when a textfield is tapped. We’ve also implemented methods to create and update the datepicker and timepicker instances.

Note that these examples are simplified and may need to be modified to fit your specific requirements. Additionally, you’ll need to implement the datePicker:didSelectDate:timeZone:forView: and timePickerDidChange: delegate methods to handle date selection and time selection, respectively.


Last modified on 2025-04-17