Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing UITextField input with a UIDatePicker

I have a table view controller with (among others) a cell that is to represent a date. I followed this post "How do I make a modal date picker that only covers half the screen?" and I have it working with one big exception - I can't get the picker to disappear!

I tried registering for the event UIControlEventTouchUpOutside, but it seems that this is not generated by the picker (or at least in the mode that I am using it).

How can I recognize that the user has finished selecting a date?

Also, is there a way to disable the user from inputting directly into the UITextField? I want to force them to use the picker. I saw this post "Disable blinking cursor in UITextField?", but is there another way?

Reagards,

--John

like image 216
johnnyspo Avatar asked Jun 16 '12 18:06

johnnyspo


1 Answers

Try this code.. here I am putting an datepicker to a uitextfield.. it will have a done button at the top right navigation bar.. so by clicking done I will user can dismiss the datepicker.. the another best method is by putting a toolbar above the datepicker having the done button.. Try this it will work.. when changing the datepicker you can populate the text field.. Hope this helps..

see this stackoverflow link https://stackoverflow.com/a/4824319/763747 this will have the datepicker with done button as toolbar above the keybord..

#pragma mark -
#pragma mark - TextField Delegate

- (BOOL)textFieldShouldReturn:(UITextField *)textField 
{
    [textField resignFirstResponder];
    return TRUE;
}


- (void) textFieldDidBeginEditing:(UITextField *)textField {
    itsRightBarButton.title  = @"Done";
    itsRightBarButton.style = UIBarButtonItemStyleDone;
    itsRightBarButton.target = self;
    itsRightBarButton.action = @selector(doneAction:);
    if ([textField isEqual:itsIncidentDateTextField])
    {
        itsDatePicker = [[[UIDatePicker alloc] init] autorelease];
        itsDatePicker.datePickerMode = UIDatePickerModeDate;
        [itsDatePicker addTarget:self action:@selector(incidentDateValueChanged:) forControlEvents:UIControlEventValueChanged];
        //datePicker.tag = indexPath.row;
        textField.inputView = itsDatePicker;
    }
}

- (IBAction) incidentDateValueChanged:(id)sender{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"MMM d, yyyy"];
    itsIncidentDateTextField.text = [dateFormatter stringFromDate:[itsDatePicker date]];
    [dateFormatter release];
}
like image 100
Dilip Rajkumar Avatar answered Oct 03 '22 21:10

Dilip Rajkumar