Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display UIDatePicker if user hit the UITextfield Programmatically

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];
like image 228
user2930730 Avatar asked Jan 09 '14 05:01

user2930730


3 Answers

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];
}
like image 147
Fabio Berger Avatar answered Nov 19 '22 19:11

Fabio Berger


    UIDatePicker *picker1   = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 210, 320, 216)];
    [picker1 setDatePickerMode:UIDatePickerModeDate];
    picker1.backgroundColor = [UIColor whiteColor];
    [picker1 addTarget:self action:@selector(startDateSelected:) forControlEvents:UIControlEventValueChanged];

//    [picker1 addSubview:toolBar];
    self.birthTxt.inputView  = picker1; // Here birthTxt is Textfield Name replace with your name
like image 33
iDeveloper Avatar answered Nov 19 '22 21:11

iDeveloper


Before going to this code, you need to just know about UIActionSheet and You need to implement UIActionShetDelegate and UITextFieldDelegate In the textFieldDidBeginEditing you need to show the action sheet as below:

-(void)textFieldDidBeginEditing:(UITextField *)sender
{
   UIActionSheet *asheet = [[UIActionSheet alloc] initWithTitle:@"Select Date" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Select", nil];
  [asheet showInView:[self.view superview]];
  [asheet setFrame:CGRectMake(0, 117, 320, 383)];
}

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
  {
    UIDatePicker *pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 40, 320, 240)];

   [pickerView setMinuteInterval:5];
   [pickerView setTag: DatePickerTag];

  [actionSheet addSubview:pickerView];
  NSArray *subviews = [actionSheet subviews];

  [[subviews objectAtIndex:SelectButtonIndex] setFrame:CGRectMake(20, 250, 280, 46)];
  [[subviews objectAtIndex:CancelButtonIndex] setFrame:CGRectMake(20, 300, 280, 46)];

}
like image 2
Mani Avatar answered Nov 19 '22 19:11

Mani