Reputation: 425
i am having two textfields,fromdate and todate.how to get different dates from the same datepicker.i tried something which ended up in getting the same date in both the textfields when the date is changed.
-(IBAction)dateValueChanged:(id)sender
{
UIDatePicker *picker = (UIDatePicker *)sender;
NSDate *dateSelected1 = [picker date];
NSDate *dateSelected2 = [picker date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yyyy"];
self.fromTextField.text = [dateFormatter stringFromDate:dateSelected1];
self.toTextField.text = [dateFormatter stringFromDate:dateSelected2];
}
Upvotes: 4
Views: 4227
Reputation: 2095
A more elegant way available of implementing this without UITextFieldDelegate
and textFieldDidBeginEditing:
Just with checking firstResponder
- (IBAction)dateValueChanged:(id)sender
{
NSDate *dateSelected = [picker date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yyyy"];
if ([firstTextField isFirstResponder])
{
firstTextField.text = [dateFormatter stringFromDate:dateSelected];
}
else if ([self.endTextField isFirstResponder])
{
secondTextField.text = [dateFormatter stringFromDate:dateSelected];
}
}
Upvotes: 7
Reputation: 766
The best way to achieve this is to use inputview property available in the UITextField. Initialize your date picker once either using Outlet or programming & assign it as inputView in the textfield's delegate method "textFieldDidBeginEditing:". For assigning date you may use your own business logic before actually assigning the inputview to the textfield.
Upvotes: 1
Reputation: 12668
You could implement the UITextFieldDelegate
Protocol for both your TextFields textFieldDidBeginEditing:
Method along with setting the tag
property for each TextField so they are easily identifiable...
Then when textFieldDidBeginEditing:
is called, you could read the tag of the textfield that has begun editing and set that as a global value so you can find out what textfield the date picker should change.. example:
- (void)textFieldDidBeginEditing:(UITextField *)textField {
someGlobalNSInteger = textField.tag; //the current text field tag is now globally set
}
-(IBAction)dateValueChanged:(id)sender {
UIDatePicker *picker = (UIDatePicker *)sender;
NSDate *dateSelected1 = [picker date];
//NSDate *dateSelected1 = [picker date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yyyy"];
//self.fromTextField.text = [dateFormatter stringFromDate:dateSelected1];
//self.toTextField.text = [dateFormatter stringFromDate:dateSelected2];
UITextField *activeTextField = (UITextField*)[self viewWithTag:someGlobalNSInteger]; //gets text field what is currently being edited
[activeTextField setText:[dateFormatter stringFromDate:dateSelected1]];
}
However if you're not allowing the text field to be editable (so it can't bring the keyboard up) you may need some other way of figuring out what textField should get the updated date but i'll leave that to you.
Hope it helps
Upvotes: 4