响应者到底是什么意思?

发布于 2024-09-27 11:31:58 字数 659 浏览 3 评论 0原文

通常我们使用 resignFirstResponder 来隐藏 iPhone 应用程序中的键盘。 例如,当我们使用UISearchBar时,用户单击搜索按钮后,我们通过实现searchBarSearchButtonClicked函数来隐藏键盘,如下所示:

 -(void)searchBarSearchButtonClicked:(UISearchBar*)searchBar{   
        NSLog(@"Search Button Click, result should be show here"); 
        [searchBar setShowsCancelButton:NO animated:YES];
       [searchBar resignFirstResponder];    
        self.dictTableView.allowsSelection = YES;   
        self.dictTableView.scrollEnabled = YES;     
}

我们将searchBar设置为第一响应者,那么用户单击搜索后键盘将被隐藏按钮。

为什么它会这样工作?我们没有调用任何隐藏键盘的函数,只是将searchBar设置为第一响应者,为什么系统会自动隐藏键盘呢?

谁能解释一下这个过程的机制?

非常感谢!

Normally we use the resignFirstResponder to hide the keyboard in the iphone apps.
For example, when we use a UISearchBar, after the user click the search button, we hide the keyboard by implement the searchBarSearchButtonClicked function like this:

 -(void)searchBarSearchButtonClicked:(UISearchBar*)searchBar{   
        NSLog(@"Search Button Click, result should be show here"); 
        [searchBar setShowsCancelButton:NO animated:YES];
       [searchBar resignFirstResponder];    
        self.dictTableView.allowsSelection = YES;   
        self.dictTableView.scrollEnabled = YES;     
}

We set the searchBar as the first responder, then the keyboard will be hidden after the user click the search button.

Why it works like this? We didn't call any function that hide the keyboard, we just set the searchBar as the first responder, why the system just hide the keyboard automatically?

Could anyone explain the mechanism of this process?

Thank you very much!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

辞别 2024-10-04 11:31:58

Objective-C 的工作原理是以选择器的形式向对象发送消息,选择器是属于目标对象的方法的名称。 “响应者”是一个对 UI 事件发送的消息做出响应的对象。

响应者生活在一个称为响应者链的层次结构中。如果响应者无法理解 UI 消息,它会将其转发到链中的下一个响应者,一直到应用程序对象本身。

对于文本字段,点击该字段会导致该字段成为第一响应者,从而拦截所有后续 UI 事件。文本字段捕获键盘的 UI 事件,因此它会自动唤起键盘。文本字段对与键盘无关的任何输入不感兴趣,因此当它辞去第一响应者的角色时,它会将键盘收起来。

这就是高度面向对象的 API 的美妙之处。您不必对标准行为进行微观管理。对象本身会为您处理它。如果您习惯使用更加程序化的 API,那么可能会有点迷失方向。

(顺便说一句,现在这已经是老帽子了,但在 90 年代初,当 Objective-C 和 NextStep 都是突破性技术时,它就让粉丝们疯狂了。Tim Burners-Lee 发明了网络浏览器,只是为了让他有一个借口购买下一个立方体。)

Objective-C works by sending messages to objects in the form of selectors which are names of methods belonging to the targeted object. A "responder" is an object that will respond to a message sent in by a UI event.

Responders live in an hierarchy called the responder chain. If a responder cannot understand a UI message it forwards it to the next responder in the chain all the way up to the application object itself.

In the case of a text field, tapping the field causes the field to become the first responder such that it intercepts all subsequent UI events. A text field traps UI events with the keyboard so it automatically evokes the keyboard. A text field has no interest in any input not associated with the keyboard so when it resigns as first responder, it puts the keyboard away.

This is the beauty of a highly-object oriented API. You don't have to micromanage standard behaviors. The objects themselves handle it for you. If your used to working with a more procedural API it might be a little disorienting.

(As an aside, this is old hat these days but it was fan-fricken-tastic back in the early 90s when Objective-C and NextStep were ground breaking technologies. Tim Burners-Lee invented the web browser just so he would have an excuse to buy a Next cube.)

蓬勃野心 2024-10-04 11:31:58

“第一响应者”是当前正在接受事件(键盘或触摸事件)的 UI 对象。需要键盘输入的对象(例如搜索栏)将在成为第一响应者时导致键盘出现。

当您告诉搜索栏停止充当“第一响应者”([searchBar resignFirstResponder]) 时,不再需要键盘并消失。

The "first responder" is the UI object that's currently accepting events (keyboard or touch events). Objects that expect keyboard input (like a search bar) will cause the keyboard to appear when they become first responder.

When you tell the searchbar to stop being "first responder" ([searchBar resignFirstResponder]), the keyboard is no longer needed and goes away.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文