Ben Lu
Ben Lu

Reputation: 3042

Subclass a UITextField and detect when it becomes or resigns first responder

I subclassed a UITextField and wish to get a method called when it becomes first responder or resigns first responder. How can I achieve this?

Upvotes: 4

Views: 5231

Answers (2)

Matt
Matt

Reputation: 1586

Just override becomeFirstResponder to call your method. Something like,

    - (BOOL)becomeFirstResponder
    {
        BOOL returnValue = [super becomeFirstResponder];
        if (returnValue) {
            [self method];
        }
        return returnValue;
    }

See the docs here for more info on responder methods: http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIResponder_Class/Reference/Reference.html#//apple_ref/occ/cl/UIResponder

Upvotes: 12

truthful_ness
truthful_ness

Reputation: 84

This does not work for me. I had to become the text field's delegate and implement this delegate method:

- (void)textFieldDidBeginEditing:(UITextField *)textField{
     //call some custom code here...
}

Upvotes: 1

Related Questions