【问题标题】:Record each character from UITextfield into NSDictionary in IOS将UITextfield中的每个字符记录到IOS中的NSDictionary中
【发布时间】:2017-03-31 05:51:29
【问题描述】:

我正在使用 UICollectionview,每个单元格都有 UITextfield,我正在尝试从每个文本字段中获取完整的字符串并保存到字典中。我使用了以下代码

  - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if (textField.tag==100) {
         [m11ThumbImage_MutCaptionDictionary setValue:[textField.text stringByAppendingString:string] forKey:@"image_0001"];
    }
    if (textField.tag==101) {
        [m11ThumbImage_MutCaptionDictionary setValue:[textField.text stringByAppendingString:string] forKey:@"image_0002"];
    }
    }

我试过了:

使用上面的代码,我可以正确设置字符串,但是,如果我删除字符,上面的方法不会被调用并且字典不会更新。

TextFieldDidBeginEditing 最初调用并且只能记录 1 个字符。还尝试了其他委托方法

预期输出:

{
image_0001: All Character,
image_0002: A
}

任何帮助都会被投票。谢谢。

【问题讨论】:

    标签: ios objective-c uitextfield uicollectionviewcell


    【解决方案1】:

    textField shouldChangeCharactersInRangetext field 实际更改其文本之前被调用,如果您需要获取更新值,那么就这样做

    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
          
        if (textField.tag==100) {
             [m11ThumbImage_MutCaptionDictionary setValue:[textField.text stringByReplacingCharactersInRange:range withString:string] forKey:@"image_0001"];
        }
        else if (textField.tag==101) {
        [m11ThumbImage_MutCaptionDictionary setValue:[textField.text stringByReplacingCharactersInRange:range withString:string] forKey:@"image_0002"];
    }
    }
    

    更新答案

     - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
          
            NSString * currentStr = [textField.text stringByReplacingCharactersInRange:range withString:string];
    
        if (textField.tag==100) {
             [m11ThumbImage_MutCaptionDictionary setValue:currentStr forKey:@"image_0001"];
        }
        else if (textField.tag==101) {
        [m11ThumbImage_MutCaptionDictionary setValue:currentStr forKey:@"image_0002"];
         }
      return YES;
    }
    

    替代方式

    在你的 viewdidload 中添加要获得EditingChanged textfield event,我建议将选择器添加为

    [textField addTarget:self action:@selector(textChanged:) forControlEvents:UIControlEventEditingChanged];
    

    随着文本字段值的更改,您将从textChanged: 选择器中获得更改后的值。

     -(void)textChanged:(UITextField *)textField
       {
        NSLog(@"textfield data %@ ",textField.text);
        if (textField.tag==100) {
         [m11ThumbImage_MutCaptionDictionary setValue:[textField.text stringByAppendingString:string] forKey:@"image_0001"];
       }
     else if (textField.tag==101) {
        [m11ThumbImage_MutCaptionDictionary setValue:[textField.text stringByAppendingString:string] forKey:@"image_0002"];
    }
    }
    

    【讨论】:

    • 如果我删除字符,shouldChangeCharactersInRange 不会调用。我无法记录他们如何解决这个问题?
    • 您是否设置了文本字段的代表
    • 是的。这就是为什么 shouldChangeCharactersInRange 调用
    • @AvijitNagare - 我的兄弟,shouldChangeCharactersInRange 是捕获当前文本的最佳位置
    • @AvijitNagare = 否则我们会选择其他想法等待
    猜你喜欢
    • 2013-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-17
    • 2012-07-31
    • 1970-01-01
    • 2013-09-13
    相关资源
    最近更新 更多