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

Advertisements

7 thoughts on “iPhone: TextField custom textDidChange action

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

    1. Actually, the code where it said textFieldDidChange: was correct. The extra semicolon was for the following method. -(void)textFieldDidChange:id(sender)

  2. Bonjour,
    “UIControllEventEditingChanged” Undeclared

    Where and How to declare it ?

    Excuse me I’m French and novice.
    Thank you.

  3. No, it is OK
    C’était parce que j’avais réalisé un “copier Coller” qui m’avait restitué l’erreur “COntrol” et non “Control”.
    Sorry

  4. Just a typo error. “UIControllEventEditingChanged” notice the double “ll” “UIContro **here** ll **here** EventEditingChanged”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s