How to display UIDatePicker if user hit the UITextfield Programmatically
ios, iphone, objective-c, xcode
Solution
The easiest way is to implement the `datePicker` is as following:
Create the `datePicker:`
self.datePicker = [[UIDatePicker alloc] initWithFrame:CGRectZero];
[self.datePicker setDatePickerMode:UIDatePickerModeDate];
[self.datePicker addTarget:self action:@selector(onDatePickerValueChanged:) forControlEvents:UIControlEventValueChanged];
Then link the `datePicker` to the `textfield` inputView:
self.textField.inputView = self.pickerView;
In the end you can catch the action when a date was picked, and convert the date to a string, to show the selected date in the `textField`
- (void)onDatePickerValueChanged:(UIDatePicker *)datePicker
{
self.textField.text = [self.dateFormatter stringFromDate:datePicker.date];
}
Problem
I'd like to Display UIDatePicker only when user the UITextField is Clicked. When the date is picked, it should be shown in the same UITextField.I want to implement UIDatePicker programmatically.I know how to write the code of UITextField Programmatically. I even don't know how to call the UIDatePicker. Date of Birth.m ``` UITextField txtDOB=[[UITextField alloc]initWithFrame:CGRectMake(10, 190, 300, 20)]; txtDOB.borderStyle=UITextBorderStyleNone; txtDOB.backgroundColor=[UIColor clearColor]; [txtDOB setUserInteractionEnabled:NO]; txtDOB.font=[UIFont systemFontOfSize:14.0]; txtDOB.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter; txtDOB.textAlignment=NSTextAlignmentLeft; txtDOB.delegate=self; [self.view addSubview:txtDOB]; ```