Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Focus on next valid key view on iPhone

Is there an iPhone equivalent for the NSResponder methods -selectNextKeyView or -nextValidKeyView from Mac OS X? I know about the -becomeFirstResponder method, but finding out which view to call that on is not very pretty when view hierarchies get more complicated.

There must be some kind of way to find this out as when I press tab when in the iPhone Simulator, focus does properly go to the next UITextField. This made me wonder what exactly happens when I press tab. Any ideas?

Update: This does exactly what I want, but _nextKeyResponder is private API, so a no-no. Is there any way to do a 'fake' tab key press without using private API?

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    // Try to find next responder
    UIView *nextResponder = (UIView *)[self.view _nextKeyResponder];

    if (nextResponder) {
        // Found next responder, so set it.
        [nextResponder becomeFirstResponder];
        [self.tableView scrollRectToVisible:[self.tableView convertRect:[nextResponder frame] fromView:nextResponder] animated:YES];
    } else {
        // Not found, so remove keyboard.
        [textField resignFirstResponder];
    }
    return NO; // We do not want UITextField to insert line-breaks.
}
like image 217
Johan Kool Avatar asked Jul 13 '10 22:07

Johan Kool


People also ask

What does focus mean on iPhone Messages?

Focus is an iPhone feature that lets you block all notifications except from specific people or apps. Introduced in iOS 15, Focus is like an advanced version of Do Not Disturb. Some Focus options also let you automatically reply with a prewritten message to anyone who texts you.

What happens when you turn off focus on iPhone?

When you're finished using a Focus, you can quickly turn it off to allow notifications again. After you turn off a Focus, it still appears in Control Center and can be reused. Do any of the following: Touch and hold the Focus icon on the Lock Screen.


1 Answers

There is not a public iOS equivalent for NSResponder's -selectKeyView or -nextValidKeyView.

When the first responder is an instance of UITextField, pressing tab instantiates a private subclass of UIEvent which is passed to -[UIApplication sendEvent:], which in turn calls -[UIView _nextKeyResponder].

-[UIView _nextKeyResponder] doesn't work quite the way you think it does. It treats the key view chain as a loop, so your else block will never be reached. For the same reason, even if there was a public API for synthesizing keyboard events, you probably wouldn't want to use it.

Instead, you probably want something more like UIWebView's UIToolbar-based form input accessory. Its buttons can be enabled and disabled when appropriate, and its delegate handles the actual button press actions.

To implement such a delegate in a general way, however, it might be helpful to look at how -[UIView _nextKeyResponder] is implemented.

like image 51
lemnar Avatar answered Oct 24 '22 02:10

lemnar