1

1 つの UIViewController に 2 つの 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];    


}
4

2 に答える 2

2

タグを使用して異なるテキストフィールドを識別します。次に、(textfield.tag==something) が何かを行うかどうかを確認できます

于 2012-05-19T07:44:49.273 に答える
1
-(BOOL)textFieldShouldBeginEditing:(UITextField*)textField {
    [txtField addTarget:self action:@selector(setPicker:)forControlEvents:UIControlEventEditingDidBegin];
}

-(void)setPicker:(id)sender
{
    if ([sender tag] == 1) { //first textField tag
        //textField 1
    }
    else {
       //textField 2
    }
}
于 2012-05-19T07:51:26.567 に答える