Brian Liang
Brian Liang

Reputation: 7774

UITextField issue

I have a navigation controller with a controller A having a UITextField.

In another controller view B, I call a public method on the controller A to clear the text on the UITextField.

In controller A, there's a delegate that is implemented:

-(BOOL)textField: (UITextField *)textField 
 shouldChangeCharactersInRange: (NSRange)range 
 replacementString: (NSString *)string

When I'm attempting to clear the UITextField (not in view), I'm unable to trigger the shouldChangeCharactersInRange delegate. Only when UITextField is in view that it works as advertised.

I'm wondering, since it isn't in view when I'm clearing the UITextField that there are implications that I am unaware of? Is this by design?

Basically setting the property of the textfield will not trigger the textfield. Only keyboard entries will.

Upvotes: 0

Views: 254

Answers (2)

kennytm
kennytm

Reputation: 523154

From the doc:

The text field calls this method whenever the user types a new character in the text field or deletes an existing character.

not the programmer. So it's by-design.

Upvotes: 1

Costique
Costique

Reputation: 23722

I've always been under the impression that programmatically changing UITextField's text property does not trigger -textField:shouldChangeCharactersInRange:replacementString:. If clearing the text in the text field is not enough for you, I think you should execute the appropriate code manually too.

Upvotes: 0

Related Questions