objective-cuikittvosapple-tv

Programmatically change focus after input text in UITextfield


I have a simple view which has two textviews as well as a button. All controls are positioned vertically. I can navigate from one control to another by using the remote. However, I'm trying to get the following scenario working:

  1. The user launches the app (focus is on first textfield)
  2. The user enters the textfield and uses the fullscreen keyboard, then she hits the "Next" button
  3. The app shows the second textfield (fullscreen keyboard), types in some text and hits the "Done" button
  4. The app leaves the fullscreen keyboard and focuses the button

While step 1-3 is working out of the box, changing the focus programatically (4) doesn't work. Here's some code:

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    if(textField == self.textfieldB) {
        self.activeView = self.textfieldB;
        [self setNeedsFocusUpdate];
        [self updateFocusIfNeeded];
        return YES;
    } 
    return NO;
}

and in - (UIView *)preferredFocusedView I'm returning whatever is stored in self.activeView (yes, it will return the button instance). All methods are called as expected, however the focus doesn't change. What am I doing wrong here?

Thanks in advance!


Solution

  • According to this developer forums thread the behaviour is intentional:

    When dismissing a presented view controller, like a fullscreen keyboard, UIKit will automatically return focus back to where it was when that view controller was first presented, if that view is still focusable. This is so that focus automatically returns back to where the user would expect it to be. There is currently no mechanism for overriding this behavior.

    In a similar case I switched the focus with a delay after returning from the keyboard screen. It’s an ugly hack, I wish UIKit wasn’t so opinionated about this.