【问题标题】:Determine if a particular UITextField is selected确定是否选择了特定的 UITextField
【发布时间】:2012-05-19 17:28:36
【问题描述】:

我在一个 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];    


}

【问题讨论】:

    标签: iphone ios uitextfield


    【解决方案1】:

    使用标签来识别不同的文本字段。然后你可以检查 (textfield.tag==something) 是否做某事

    【讨论】:

      【解决方案2】:
      -(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
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2017-04-12
        • 1970-01-01
        • 1970-01-01
        • 2017-03-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-24
        • 1970-01-01
        相关资源
        最近更新 更多