专注于iPhone上的下一个有效键视图 [英] Focus on next valid key view on iPhone

查看:97
本文介绍了专注于iPhone上的下一个有效键视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

NSResponder 方法 -selectNextKeyView -nextValidKeyView 从Mac OS X?我知道 -becomeFirstResponder 方法,但是当视图层次结构变得更复杂时,找出调用该视图的视图不是很漂亮。



在iPhone模拟器中,当我按下Tab键时,必须有某种方法找到这个方法,焦点会正确到下一个 UITextField 。这让我想知道当我按Tab键时会发生什么。任何想法?



更新:这正是我想要的,但 _nextKeyResponder 私人API,所以一个no-no。有没有办法做一个'假的'标签键按不使用私人API?

   - (BOOL)textFieldShouldReturn:(UITextField *)textField {
//尝试查找下一个响应者
UIView * nextResponder =(UIView *)[self.view _nextKeyResponder];

if(nextResponder){
//找到下一个响应者,因此设置它。
[nextResponder becomeFirstResponder];
[self.tableView scrollRectToVisible:[self.tableView convertRect:[nextResponder frame] fromView:nextResponder] animated:YES];
} else {
//找不到,所以删除键盘。
[textField resignFirstResponder];
}
return NO; //我们不希望UITextField插入换行符。
}


解决方案

等效于 NSResponder -selectKeyView -nextValidKeyView



当第一个响应程序是 UITextField 的实例时,按Tab键实例化 [UIView _nextKeyResponder] ,传递给 - [UIApplication sendEvent:] 的UIEvent code>。



- [UIView _nextKeyResponder] 不能以您认为的方式工作。它将键视图链视为一个循环,所以你的 else 块永远不会被访问。出于同样的原因,即使有一个公共的API来合成键盘事件,你可能不想使用它。



相反,你可能想要更像 UIWebView 基于UIToolbar 的表单输入附件。



但是,为了以一般的方式实现这样的代理,它可以是有助于了解如何实施 - [UIView _nextKeyResponder] 。 p>

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

解决方案

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.

这篇关于专注于iPhone上的下一个有效键视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆