如何在Array of Textfield中修改选定的UITexFiled值

时间:2022-08-16 18:54:40

Currently, I created Array of UITextField based on Response from Server in my UI, My response contains three types of response in single API, i.e I get 5 key and Values. Values contains Types like String, Date, Array, based on this when I select the UITextFiled the value must change according to that Particular TextFiled

目前,我在我的UI中基于Server的响应创建了UITextField数组,我的响应在单个API中包含三种类型的响应,即我得到5个键和值。值包含类型,如String,Date,Array,基于此,当我选择UITextFiled时,值必须根据特定TextFiled更改

Here my Sample Code:

这是我的示例代码:

   for (int i=0;i<itemAttributeArray.count;i++){
        UIColor *floatingLabelColor = [UIColor brownColor];
        textField1 = [[JVFloatLabeledTextField alloc] initWithFrame:CGRectMake(16, y, width, height)];
        textField1.delegate = self;
        //Set tag 101
        textField1.tag = 101;
        NSLog(@"textField1.tag - %ld",(long)textField1.tag);
        textField1.text = [[itemAttributeArray valueForKey:@"value"]objectAtIndex:i];
        [self SetTextFieldBorder:textField1];
        textField1.placeholder = [keyArr objectAtIndex:i];

        textField1.font = [UIFont systemFontOfSize:kJVFieldFontSize];
        //            textField1.clearsOnBeginEditing = YES;
        textField1.clearButtonMode = UITextFieldViewModeWhileEditing;

        textField1.floatingLabelFont = [UIFont boldSystemFontOfSize:kJVFieldFloatingLabelFontSize];
        textField1.floatingLabelTextColor = floatingLabelColor;

  //      textField1.translatesAutoresizingMaskIntoConstraints = NO;

        [textField1 resignFirstResponder];

        [_scroll addSubview:textField1];
        [textFields addObject:textField1];
        [textField1 release];

        y += height + margin;

        if ([[[itemAttributeArray valueForKey:@"type"] objectAtIndex:i] isEqualToString:@"string"]){
            NSLog(@"type - %@",[[itemAttributeArray valueForKey:@"type"] objectAtIndex:i]);

        }else if ([[[itemAttributeArray valueForKey:@"type"] 
 objectAtIndex:i] isEqualToString:@"date"]){
   NSLog(@"type - %@",[[itemAttributeArray valueForKey:@"type"] objectAtIndex:i]);
            textField1.tag = 102;
            [textField1 addTarget:self action:@selector(textFieldDidChange_dateChek)
                         forControlEvents:UIControlEventEditingDidBegin];
        }else if ([[[itemAttributeArray valueForKey:@"type"] objectAtIndex:i] isEqualToString:@"double"]){
             NSLog(@"type - %@",[[itemAttributeArray valueForKey:@"type"] objectAtIndex:i]);
        }

-(void)textFieldDidChange_dateChek{
NSLog(@"iam called on first edit");
 _picker_uiView.hidden = false;
[_datePickerView addTarget:self action:@selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged];
}



 - (void)datePickerValueChanged:(id)sender {
   NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  [dateFormatter setDateFormat:@"h:mm a"];

   textField1.text = [dateFormatter 
  stringFromDate:_datePickerView.date];
   }

When I select the date value is entered in the last object to UITextfield but I selected index is 2nd but values changing on the last element of UITextfield

当我选择在UITextfield的最后一个对象中输入日期值但是我选择的索引是第二个但是在UITextfield的最后一个元素上改变了值

1 个解决方案

#1


1  

you are create the textfield in globally, thats the reason you get the last index only. in here assign the tag for each textfield as well as create the instance value.for e.g

你是在全局创建文本域,这就是你只获得最后一个索引的原因。在这里为每个文本字段分配标记以及创建实例值。例如

 for (int i=0;i<itemAttributeArray.count;i++){
        UIColor *floatingLabelColor = [UIColor brownColor];
       JVFloatLabeledTextField *textField1 = [[JVFloatLabeledTextField alloc] initWithFrame:CGRectMake(16, y, width, height)];
        textField1.delegate = self;
        //Set tag 101
        textField1.tag = 101 + i;
        NSLog(@"textField1.tag - %ld",(long)textField1.tag);
        textField1.text = [[itemAttributeArray valueForKey:@"value"]objectAtIndex:i];
        [self SetTextFieldBorder:textField1];
        textField1.placeholder = [keyArr objectAtIndex:i];

        textField1.font = [UIFont systemFontOfSize:kJVFieldFontSize];
        //            textField1.clearsOnBeginEditing = YES;
        textField1.clearButtonMode = UITextFieldViewModeWhileEditing;

        textField1.floatingLabelFont = [UIFont boldSystemFontOfSize:kJVFieldFloatingLabelFontSize];
        textField1.floatingLabelTextColor = floatingLabelColor;

  //      textField1.translatesAutoresizingMaskIntoConstraints = NO;

        [textField1 resignFirstResponder];

        [_scroll addSubview:textField1];
        [textFields addObject:textField1];
        [textField1 release];

        y += height + margin;

        if ([[[itemAttributeArray valueForKey:@"type"] objectAtIndex:i] isEqualToString:@"string"]){
            NSLog(@"type - %@",[[itemAttributeArray valueForKey:@"type"] objectAtIndex:i]);

        }else if ([[[itemAttributeArray valueForKey:@"type"] objectAtIndex:i] isEqualToString:@"date"]){
  NSLog(@"type - %@",[[itemAttributeArray valueForKey:@"type"] objectAtIndex:i]);



            [textField1 addTarget:self action:@selector(textFieldDidChange_dateChek:)
                         forControlEvents:UIControlEventEditingDidBegin];
        }else if ([[[itemAttributeArray valueForKey:@"type"] objectAtIndex:i] isEqualToString:@"double"]){

        }
              }

and handle the textfield action

并处理文本字段操作

  -(void)textFieldDidChange_dateChek:(JVFloatLabeledTextField*)textfield{
NSLog(@"iam called on first edit");
 _picker_uiView.hidden = false;

_datePickerView.tag = textfield.tag;
[textfield resignFirstResponder];
[_datePickerView addTarget:self action:@selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged];
textfield.inputView = _datePickerView;
 }

finally assign the value to the textfield as like follow

最后将值分配给文本字段,如下所示

- (void)datePickerValueChanged:(UIDatePicker*)sender {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"h:mm a"];
 //  textField1.tag = 102;
for(id aSubView in [_scroll subviews]){
    if([aSubView isKindOfClass:[JVFloatLabeledTextField class]])
    {
          JVFloatLabeledTextField *textFds=(JVFloatLabeledTextField*)aSubView;
        if (textFds.tag == sender.tag) {
             NSLog(@"dad == %@",[dateFormatter stringFromDate:sender.date]);
            textFds.text = [dateFormatter stringFromDate:sender.date];
            [textFds resignFirstResponder];
            [sender removeFromSuperview];
            break;
        }
    }

        }
}

#1


1  

you are create the textfield in globally, thats the reason you get the last index only. in here assign the tag for each textfield as well as create the instance value.for e.g

你是在全局创建文本域,这就是你只获得最后一个索引的原因。在这里为每个文本字段分配标记以及创建实例值。例如

 for (int i=0;i<itemAttributeArray.count;i++){
        UIColor *floatingLabelColor = [UIColor brownColor];
       JVFloatLabeledTextField *textField1 = [[JVFloatLabeledTextField alloc] initWithFrame:CGRectMake(16, y, width, height)];
        textField1.delegate = self;
        //Set tag 101
        textField1.tag = 101 + i;
        NSLog(@"textField1.tag - %ld",(long)textField1.tag);
        textField1.text = [[itemAttributeArray valueForKey:@"value"]objectAtIndex:i];
        [self SetTextFieldBorder:textField1];
        textField1.placeholder = [keyArr objectAtIndex:i];

        textField1.font = [UIFont systemFontOfSize:kJVFieldFontSize];
        //            textField1.clearsOnBeginEditing = YES;
        textField1.clearButtonMode = UITextFieldViewModeWhileEditing;

        textField1.floatingLabelFont = [UIFont boldSystemFontOfSize:kJVFieldFloatingLabelFontSize];
        textField1.floatingLabelTextColor = floatingLabelColor;

  //      textField1.translatesAutoresizingMaskIntoConstraints = NO;

        [textField1 resignFirstResponder];

        [_scroll addSubview:textField1];
        [textFields addObject:textField1];
        [textField1 release];

        y += height + margin;

        if ([[[itemAttributeArray valueForKey:@"type"] objectAtIndex:i] isEqualToString:@"string"]){
            NSLog(@"type - %@",[[itemAttributeArray valueForKey:@"type"] objectAtIndex:i]);

        }else if ([[[itemAttributeArray valueForKey:@"type"] objectAtIndex:i] isEqualToString:@"date"]){
  NSLog(@"type - %@",[[itemAttributeArray valueForKey:@"type"] objectAtIndex:i]);



            [textField1 addTarget:self action:@selector(textFieldDidChange_dateChek:)
                         forControlEvents:UIControlEventEditingDidBegin];
        }else if ([[[itemAttributeArray valueForKey:@"type"] objectAtIndex:i] isEqualToString:@"double"]){

        }
              }

and handle the textfield action

并处理文本字段操作

  -(void)textFieldDidChange_dateChek:(JVFloatLabeledTextField*)textfield{
NSLog(@"iam called on first edit");
 _picker_uiView.hidden = false;

_datePickerView.tag = textfield.tag;
[textfield resignFirstResponder];
[_datePickerView addTarget:self action:@selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged];
textfield.inputView = _datePickerView;
 }

finally assign the value to the textfield as like follow

最后将值分配给文本字段,如下所示

- (void)datePickerValueChanged:(UIDatePicker*)sender {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"h:mm a"];
 //  textField1.tag = 102;
for(id aSubView in [_scroll subviews]){
    if([aSubView isKindOfClass:[JVFloatLabeledTextField class]])
    {
          JVFloatLabeledTextField *textFds=(JVFloatLabeledTextField*)aSubView;
        if (textFds.tag == sender.tag) {
             NSLog(@"dad == %@",[dateFormatter stringFromDate:sender.date]);
            textFds.text = [dateFormatter stringFromDate:sender.date];
            [textFds resignFirstResponder];
            [sender removeFromSuperview];
            break;
        }
    }

        }
}