我在一个 UIViewController 中有两个 UITextField。我在两个文本字段中都打开了一个 UIDatePicker。我想更新文本字段中日期的值,但我不知道如何检查从哪个文本字段的日期选择器打开。有人可以解释一下我如何确定选择了哪个文本字段吗?
- (IBAction)selectATime:(UIControl *)sender {
[self showActionSheetForTimePicker:sender];
}
- (IBAction)showActionSheetForTimePicker:(id)sender
{
//add the action sheet
actionSheetForiPhone = [[UIActionSheet alloc] initWithTitle:@""
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
// Add the picker
pickerForiPhone = [[UIDatePicker alloc] init];
pickerForiPhone.datePickerMode = UIDatePickerModeTime;
[actionSheetForiPhone addSubview:pickerForiPhone];
[actionSheetForiPhone showInView:self.view];
[actionSheetForiPhone setBounds:CGRectMake(0, 0, 320, 520)];
[pickerForiPhone setBounds:CGRectMake(0, 0, 320, 340)];
//adding toolbar to the action sheet
UIToolbar * toolbar = [[UIToolbar alloc] initWithFrame: CGRectMake(0, 0, 320, 45)];
toolbar.barStyle = UIBarStyleBlackOpaque;
NSMutableArray *barItems = [[NSMutableArray alloc] init];
//adding buttons to the toolbar
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(dismissPicker:)];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(updateTextField:)];
[barItems addObject:cancelButton];
[barItems addObject:flexSpace];//for adding flexible space between cancel and done button
[barItems addObject:doneButton];
[toolbar setItems:barItems];
[actionSheetForiPhone addSubview:toolbar];
CGRect pickerRect = pickerForiPhone.bounds;
pickerRect.origin.y = -100;
pickerForiPhone.bounds = pickerRect;
[pickerForiPhone release];
[actionSheetForiPhone release];
}
- (void)updateTextField:(id) sender
{
//for updating the value of the textfield
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat: @"hh:mm a"];
NSString *time = [df stringFromDate:self.pickerForiPhone.date];
NSLog(@"Time = %@", time);
//if([element respondsToSelector:@selector(setText:)])
{
if(activeTextField == self.fromTime)
self.fromTime.text = time;
else
self.toTime.text = time;
}
[df release];
//for dismissing the date picker
[self.actionSheetForiPhone dismissWithClickedButtonIndex:1 animated:YES];
}