我正在实现以下方法以在更改为_detailsDictionary
的情况下保存文本字段的数据,但是当保存时,不会在textfield中输入最后一个字母。text不会出现,例如,如果我保存Krish
,则仅会保存kris
。如何解决这些问题?
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSInteger field = textField.tag;
if(field ==1)
{
if(textField.text ==[_detailsDictionary valueForKey:@"FName"])
{
NSLog(@"cell1.userDetailTextField.text %@",textField.text);
}
else
{
[_detailsDictionary setObject:textField.text forKey:@"FName"];
}
return YES;
}
if(field ==2)
{
if(textField.text ==[_detailsDictionary valueForKey:@"MName"])
{
NSLog(@"cell1.userDetailTextField.text %@",textField.text);
}
else
{
[_detailsDictionary setObject:textField.text forKey:@"MName"];
}
return YES;
}
if(field ==3)
{
if(textField.text ==[_detailsDictionary valueForKey:@"LName"])
{
NSLog(@"cell1.userDetailTextField.text %@",textField.text);
}
else
{
[_detailsDictionary setObject:textField.text forKey:@"LName"];
}
return YES;
}
if(field ==4)
{
if(textField.text ==[_detailsDictionary valueForKey:@"City"])
{
NSLog(@"cell1.userDetailTextField.text %@",textField.text);
}
else
{
[_detailsDictionary setObject:textField.text forKey:@"City"];
}
return YES;
}
if(field ==5)
{
if(textField.text ==[_detailsDictionary valueForKey:@"State"])
{
NSLog(@"cell1.userDetailTextField.text %@",textField.text);
}
else
{
[_detailsDictionary setObject:textField.text forKey:@"State"];
}
return YES;
}
if(field ==6)
{
if(textField.text ==[_detailsDictionary valueForKey:@"Country"])
{
NSLog(@"cell1.userDetailTextField.text %@",textField.text);
}
else
{
[_detailsDictionary setObject:textField.text forKey:@"Country"];
}
return YES;
}
if(field ==7)
{
if(textField.text ==[_detailsDictionary valueForKey:@"DOB"])
{
NSLog(@"cell1.userDetailTextField.text %@",textField.text);
}
else
{
[_detailsDictionary setObject:textField.text forKey:@"DOB"];
}
return YES;
}
return NO;
}
最佳答案
在shouldChangeCharactersInRange
方法中,您不会从textfield.text
获取全文,因为在文本实际输入textfield
之前将调用此方法
您需要使用
NSString *strText = [textField.text stringByReplacingCharactersInRange:range withString:string];
在
strText
中,您将获得fullText
关于ios - iOS-保存时最后一个字符消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40861610/