i am new to iPhone development. I want to add a UIDatePicker when textfield is selected.
two date picker is there.
- (void)textFieldDidBeginEditing:(UITextField *)aTextField
{
[aTextField resignFirstResponder];
pickerViewPopup = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
UIDatePicker *pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 44, 0, 0)];
pickerView.datePickerMode = UIDatePickerModeDate;
pickerView.hidden = NO;
pickerView.date = [NSDate date];
UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
pickerToolbar.barStyle = UIBarStyleBlackOpaque;
[pickerToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneButtonPressed:)];
[barItems addObject:doneBtn];
UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelButtonPressed:)];
[barItems addObject:cancelBtn];
[pickerToolbar setItems:barItems animated:YES];
[pickerViewPopup addSubview:pickerToolbar];
[pickerViewPopup addSubview:pickerView];
[pickerViewPopup showInView:self.view];
[pickerViewPopup setBounds:CGRectMake(0,0,320, 464)];
}
I used this but got same value for the two different datepickers. I want that if I press textfield1 then datepicker opens and saves that value in textfield one and same for the second textfield . Thanks.
@Maulik:
First thing
- (void)textFieldDidBeginEditing:(UITextField *)aTextFieldis called every time the textfield is selected for editing.So for both your selection i.e. textfield1 and textfield2 same code is going to be called.
What you can do is at the tap of done button, Check which textfield has opened up the picker and save the value in that textfield.
What I meant is take a textfield in .h file
UITextField *selectedTextField;Then in
textFieldDidBeginEditingmethod set it asselectedTextField = aTextField;and at the tap of done button i.e in
doneButtonPressedset the selected value forselectedTextField.text. And thats it.. you are done 🙂