如何修改文本字段数组中选定的 UITexFiled 值

Posted

技术标签:

【中文标题】如何修改文本字段数组中选定的 UITexFiled 值【英文标题】:How to modify selected UITexFiled value in Array of Textfield 【发布时间】:2017-06-05 07:26:35 【问题描述】:

目前,我在 UI 中基于来自服务器的响应创建了 UITextField 数组,我的响应在单个 API 中包含三种类型的响应,即我得到 5 个键和值。值包含字符串、日期、数组等类型,基于此,当我选择 UITextFiled 时,值必须根据特定的 TextFiled 更改

这是我的示例代码:

   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];
   

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

【问题讨论】:

【参考方案1】:

您是在全局范围内创建文本字段,这就是您仅获得最后一个索引的原因。在这里为每个文本字段分配标签并创建实例值。例如

 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"])

        
              

并处理文本字段操作

  -(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;
 

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

- (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;
        
    

        

【讨论】:

以上是关于如何修改文本字段数组中选定的 UITexFiled 值的主要内容,如果未能解决你的问题,请参考以下文章

SwiftUI - 如何修改数组中的文本字段

如何在 iOS 中找到选定的文本字段

如何在 Thymeleaf 中显示从“选择”到“文本字段”的选定项目?

如何将选定的 JComboBox 数据项提取到 Swing 文本字段中?

使用来自单独的表视图控制器的选定单元格填充文本字段

ReactJS:如何从输入字段的下拉列表中显示选定的值?