【问题标题】:How to modify selected UITexFiled value in Array of Textfield如何修改文本字段数组中选定的 UITexFiled 值
【发布时间】: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 的最后一个元素上的值发生变化时

【问题讨论】:

    标签: ios objective-c delegates 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;
            }
        }
    
            }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-05
      • 1970-01-01
      • 2020-10-17
      • 1970-01-01
      • 2018-09-23
      • 1970-01-01
      • 1970-01-01
      • 2015-05-24
      相关资源
      最近更新 更多