Home > iPhone, mac > iPhone: TextField custom textDidChange action

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;
}

  1. October 4, 2009 at 10:30 am | #1

    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];

    • October 4, 2009 at 5:43 pm | #2

      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.

  1. No trackbacks yet.