在 IOS 中将 UITextfield 中的每个字符记录到 NSDictionary 中
Record each character from UITextfield into NSDictionary in IOS
我正在使用 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
}
任何帮助都会被赞成。谢谢。
textField shouldChangeCharactersInRange
在 text 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"];
}
}
我正在使用 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
}
任何帮助都会被赞成。谢谢。
textField shouldChangeCharactersInRange
在 text 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"];
}
}