Ron
Ron

Reputation: 1269

iOS 8 Keyboard Extension Detecting first responder change

I'm writing a keyboard extension for iOS (hence overriding UIInputViewController) and I'm trying to figure out how to detect when the first responder changes. Is this even possible?

My motivation is that when the user selects a different text input field (while the keyboard is active) the style of the keyboard might need to change to suit the attributes of that input. This can happen when there are several text fields displayed on a UI and the user first selects one (causing the keyboard to be initialized) then the user selects another with different attributes (keyboard doesn't know it).

I've looked through the methods exposed by UIInputViewController and the delegates it implements but nothing I've seen really fits the bill. The closest thing I've found is selectionDidChange on UITextInputDelegate.

Upvotes: 1

Views: 539

Answers (2)

Ron
Ron

Reputation: 1269

I found the best way to get this information is to override the UITextInputDelegate textDidChange method (UIInputViewController implements UITextInputDelegate). It turns out that textDidChange is called whenever the user switches the input text field (first responder), or when the text changes for some reason (luckily not when it is your keyboard that initiated the change).

Upvotes: 1

Dare
Dare

Reputation: 2587

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField

That should tell you when it expects to become firstResponder. A couple things to keep in mind;

*This will only be called when a UITextFied is the thing becoming firstResponder. If some other object decides to, this won't be called. It'll probably call the method below.

-(BOOL)becomeFirstResponder

*Your class must conform to the UITextFieldDelegate and all of your UITextFields must set their delegates to self. Without that they won't call shouldBeginEditing on your class.

Upvotes: 0

Related Questions