Reputation: 77
Here l1,l2,l3,l4 are UILabel
s.
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSLog(@"%d",textField.text.length);
if (textField.text.length==0)
{
l1.text=@".";
l2.text=@"__";
l3.text=@"__";
l4.text=@"__";
}else if (textField.text.length==1)
{
l1.text=@".";
l2.text=@".";
l3.text=@"__";
l4.text=@"__";
}else if (textField.text.length==2)
{
l1.text=@".";
l2.text=@".";
l3.text=@".";
l4.text=@"__";
}else if (textField.text.length==3)
{
l1.text=@".";
l2.text=@".";
l3.text=@".";
l4.text=@".";
}
NSUInteger newLength = [textField.text length] + [string length] - range.length;
return (newLength > 4) ? NO : YES;
}
Here output follows as...
textfield is 1234 then labels are . . . .
it's working fine but when the user deletes characters from the text field, I get the following results:
"123" . . . . "12" . . . . "1" . . . _ "" . . _ _
What I expect is that when the textfield contains no characters, my labels would show _ _ _ _
Upvotes: 0
Views: 284
Reputation: 9650
You want to look at the text that results from the user editing that field. To do so create a new string:
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *editedText = [textField.text stringByReplacingCharactersInRange:range withString:string];
NSLog(@"%d",editedText.length);
if (editedText.length==0)
{
l1.text=@"__";
l2.text=@"__";
l3.text=@"__";
l4.text=@"__";
}
else if (editedText.length==1)
{
l1.text=@".";
l2.text=@"__";
l3.text=@"__";
l4.text=@"__";
}else if (editedText.length==2)
{
l1.text=@".";
l2.text=@".";
l3.text=@"__";
l4.text=@"__";
}else if (editedText.length==3)
{
l1.text=@".";
l2.text=@".";
l3.text=@".";
l4.text=@"__";
}else if (editedText.length==4)
{
l1.text=@".";
l2.text=@".";
l3.text=@".";
l4.text=@".";
}
return (editedText.length > 4) ? NO : YES;
}
Upvotes: 1
Reputation: 2085
The UITextField
does not update it's text until the end of that function if you return YES. Your assuming that the textField.text is updated to reflect what you hit before that function. What you need to do is test the string
parameter and see if it is a backspace
character code. If it is then you should be testing for textField.text.length - 1 .
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSInteger offSet = 0;
if ( [string isEqualToString:@""] ) {
offset = 1;
}
if ( textField.text.length - offSet == value ) {
}
BOOL shouldChangeCharacters = YES;
return shouldChangeCharacters;
}
Upvotes: 0
Reputation: 119031
You should base your if
statements on the actual text that will be in the text field after the edit is approved and actioned. By using if (textField.text.length==0)
you are assuming that you start with nothing and add a character to get to a single character in the text field - this is not a valid assumption.
Use The current text in the textField
together with the range
and replacementString
to get the actual resulting text (stringByReplacingCharactersInRange:withString:
).
Note that this will also allow you to deal with users cutting and pasting text into the field.
Upvotes: 0