0

I want to know about the character printing in UITextField

when i start to entering text in textfield it entering perfect in box, but in NSLog it shows wrong at first it display nothing . at second character it display first character in console. i want to know why should this happening else i seen both characters in textfield.

enter image description here

this is my code of textfield delegates method

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSLog(@"print %@", textField.text);
    return YES;
}
  // this is my console output
  2012-01-04 12:40:41.736 Demo[1982:f803] print 
  2012-01-04 12:40:43.497 Demo[1982:f803] print h
Hiren
  • 12,720
  • 7
  • 52
  • 72

6 Answers6

2

That is a correct behavior as the current vaule of textField.text doesn't change until that method returns YES. If you want to NSLog the same string you can see displaying then you have to build it changing characters "string" in range "range". That is what will happen to textField.text when you return YES and what will not happen if you return NO.

Gabriel
  • 3,319
  • 1
  • 16
  • 21
2

At the time - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string is called, your textfield values hasn't yet been changed, thus it's quite a normal output you have.

You have to take the new characters from the replacementString:(NSString *)string.

Jaffa
  • 12,442
  • 4
  • 49
  • 101
  • thank you very much it's clear now in my mind. i want to know one more thing about textfield. when i remmove characters in textfield then which delegate method is called of textfiled? – Hiren Jan 04 '12 at 07:28
  • This one should also be called if I don't mistake – Jaffa Jan 04 '12 at 07:57
  • 1
    same method, with replacement string being "" for a range > 0 – bshirley Jan 04 '12 at 07:58
0

It's not wrong. It's the property of shouldChangeCharacters method. This may help: Getting the Value of a UITextField as keystrokes are entered?

Community
  • 1
  • 1
utsabiem
  • 920
  • 4
  • 10
  • 21
0

Maybe thes method called before the symbol inputed. Try to call NSLog after some delay with selector.

Padavan
  • 1,056
  • 1
  • 11
  • 32
0

_should_... indicating is hasn't yet occurred

of use:

[textField.text stringByReplacingCharactersInRange:range withString:string];
bshirley
  • 8,217
  • 1
  • 37
  • 43
0

Try to NSLog this:

[textField.text stringByReplacingCharactersInRange:range withString:string];
Mat
  • 7,613
  • 4
  • 40
  • 56