2

In my viewDidLoad I have the following Code:

UIDatePicker* datePicker = [[UIDatePicker alloc] init];
datePicker.datePickerMode = UIDatePickerModeDate;
[datePicker addTarget:self action:@selector(datePickerChanged:) forControlEvents:UIControlEventValueChanged];
dateField1.inputView = datePicker;
dateField2.inputView = datePicker;
dateField3.inputView = datePicker;
dateField4.inputView = datePicker;

(The dateField's are all UITextFields)

In my -(void)datePickerChanged:(UIDatePicker*)datePicker method, how do I know which dateField is being edited, so that I can fill in the picked date?

Currently I am using a new UIDatePicker for each UITextField, but I hope there is a more elegant solution for this situation.

Any help is appreciated!

Anusha Kottiyal
  • 3,855
  • 3
  • 28
  • 45
Mark_ObjC
  • 42
  • 6

2 Answers2

8
if([textField1 isFirstResponder]) {

}
else if[textField2 isFirstResponder]){

}

...etc...
Louis Cremen
  • 764
  • 7
  • 12
0

You can set up different tags for your dateFields and check what field was changed in datePickerChanged: method. But to do it you should store original results in another place (for example in dictionary where your dateField's tags will be a keys). I did not tried this before but should work.

anatoliy_v
  • 1,782
  • 15
  • 24
  • Thanks for your help!, but by setting the datePicker as the inputView doesn't automatically fill the textField if a value has been picked (so there is nothing changed). I am using the datePickerChanged: method to fill in the dateField. And so I still don't know which dateField is being edited – Mark_ObjC Dec 13 '12 at 20:18