iPhone: TextField custom textDidChange action
While working on a project, I came across a requirement where I was required to fire an action on textfield text change. I did not find any method that apple provides like it does for searchbar text change. With some research on google, I went ahead and created a custom method which was then added on textfield event change namely UICOntrolEventEditingChanged.
This was what I did to make it work.
In ViewDidLoad, I added the following code:
[myTextfield addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UICOntrolEventEditingChanged];
Here I am attaching ‘textFieldDidChange’ method which will be called once the text in the myTextfield changes. I named it like this to make it sound more like the method for searchbar textchange method.
The actual method is:
-(void)textFieldDidChange {
// whatever you wanted to do
someLabel.text = myTextfield.text;
}
Thanks, this was just what I was looking for.
However, I found out there were 2 small errors in your line for the viewDidLoad function.
The code should be:
[myTextfield addTarget:self action:@selector(textFieldDidChange) forControlEvents:UIControlEventEditingChanged];
Glad I cud be of some help. Actually my version works well. I wrote it on OS 2.2.1 and it does work on 3.0. Don’t know why it wont work anywhere else.