xcode 故事板编辑器中的“First Responder”和“Exit”框的用途是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13565128/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What are the "First Responder" and "Exit" boxes purpose in the storyboard editor?
提问by Madivad
In the XCode IDE, at the bottom of the view controller in the MainStoryboard editor, are two boxes: First Responder, and Exit.
在 XCode IDE 中,MainStoryboard 编辑器中视图控制器的底部有两个框:First Responder 和 Exit。
I know what a firstResponder is programatically within the code, but in the storyboard editor, I can't seem to do anything useful by it.
我知道代码中的 firstResponder 是什么,但是在故事板编辑器中,我似乎无法通过它做任何有用的事情。
Am I able to use the first responder in this area to somehow set the first responder of the view? I'd like the first textfield to be active on load and I have tried right+click and dragging to no avail. I know I can set it programatically in the viewDidLoad method, but is there some way of doing it here?
我可以使用该区域的第一响应者以某种方式设置视图的第一响应者吗?我希望第一个文本字段在加载时处于活动状态,我尝试右键单击并拖动无济于事。我知道我可以在 viewDidLoad 方法中以编程方式设置它,但是这里有什么方法可以做到吗?
And what is the green Exit for?
绿色出口是做什么用的?
采纳答案by Fogmeister
I've never used it and probably never will but you can assign an object to be the first in line to receive the events from the UI.
我从未使用过它,也可能永远不会使用它,但您可以指定一个对象作为第一个从 UI 接收事件的队列。
I suppose you could be creating a UIView subclass and add it in to a UIViewController but you actually want some other object to receive and process the events other than the UIViewController you are adding it to.
我想您可以创建一个 UIView 子类并将其添加到 UIViewController 中,但您实际上希望其他一些对象接收和处理除您添加到的 UIViewController 之外的事件。
回答by Borzh
There are no good answer for this question, so I am posting my answer:
这个问题没有很好的答案,所以我发布了我的答案:
From here:
从这里:
Note: You probably won't be using the First Responder very much. This is a proxy object that refers to whatever object has first responder status at any given time. It was also present in your nibs and you probably never had a need to use it then either. As an example, you can hook up the Touch Up Inside event from a button to First Responder's cut: selector. If at some point a text field has input focus then you can press that button to make the text field, which is now the first responder, cut its text to the pasteboard.
注意:您可能不会经常使用 First Responder。这是一个代理对象,指的是在任何给定时间具有第一响应者状态的任何对象。它也存在于您的笔尖中,那时您可能也不需要使用它。例如,您可以将按钮的 Touch Up Inside 事件连接到 First Responder 的 cut: 选择器。如果在某个时候文本字段具有输入焦点,那么您可以按下该按钮使文本字段(现在是第一响应者)将其文本剪切到粘贴板。
Edit:
编辑:
1) First Responder is very useful if you are using text fields with keyboard notifications. I use it to make keyboard disappear, make an outlet to variable currentFirstResponder of your class, and in viewWillDisappear:
1) 如果您使用带有键盘通知的文本字段,First Responder 非常有用。我用它来让键盘消失,为你班级的变量 currentFirstResponder 建立一个出口,并在 viewWillDisappear 中:
[self.currentFirstResponder resignFirstResponder];
2) You can read about unwind segues ("Exit" box) here
回答by Sayooj
First Responder:The First Responder icon stands for the object that the user is currently interacting with. When a user works with an iOS application, multiple objects could potentially respond to the various gestures or keystrokes that the user creates. The first responder is the object currently in control and interacting with the user. A text field that the user is typing into, for example, would be the first responder until the user moves to another field or control.
第一响应者:第一响应者图标代表用户当前正在与之交互的对象。当用户使用 iOS 应用程序时,多个对象可能会响应用户创建的各种手势或击键。第一响应者是当前控制并与用户交互的对象。例如,用户正在输入的文本字段将是第一响应者,直到用户移动到另一个字段或控件。
Exit:The Exit icon serves a very specific purpose that will come into play only in multiscene applications. When you are creating an app that moves the user between a series of screens, the Exit icon provides a visual means of jumping back to a previous screen. If you have built five scenes that link from one to another and you want to quickly return to the first scene from the fifth, you'll link from the fifth scene to the first scene's Exit icon.
退出:退出图标具有非常特殊的用途,仅在多场景应用程序中发挥作用。当您创建一个在一系列屏幕之间移动用户的应用程序时,退出图标提供了一种跳回前一个屏幕的视觉方式。如果您已经构建了五个相互链接的场景,并且您想从第五个场景快速返回到第一个场景,您将从第五个场景链接到第一个场景的退出图标。
More here
更多在这里
回答by malhal
You don't see this very often, where a deleted answer is actually correct, and the comment (likely influencing its deletion) on it is totally wrong! I'll try and improve on it.
您不会经常看到这种情况,删除的答案实际上是正确的,并且对其的评论(可能会影响其删除)完全错误!我会努力改进的。
Usually the IBAction
you want to hook up to a button is in the view controller containing the button. However if the IBAction
is in a different controller, e.g. a parent controller then drag from the button to the First Responder object and you are able to select the IBAction
in the parent controller!
通常,IBAction
您要连接到按钮的位置在包含该按钮的视图控制器中。但是,如果IBAction
位于不同的控制器中,例如父控制器,则从按钮拖动到 First Responder 对象,您就可以IBAction
在父控制器中选择!
As the hidden answer states, how this is implemented is the action is sent to nil, which has the effect of the responder chain (i.e. view hierarchy) being searched for the action, as follows:
正如隐藏答案所述,这是如何实现的,就是将动作发送到 nil,这具有在响应者链(即视图层次结构)中搜索动作的效果,如下所示:
[UIApplication.sharedApplication sendAction:@selector(nextObject:) to:nil from:self forEvent:nil];
[UIApplication.sharedApplication sendAction:@selector(nextObject:) to:nil from:self forEvent:nil];
An example is a custom UITableViewCell
. Add a UIButton
to the cell but you want the action to go up to a View Controller that has an embed segue to a UITableViewController
. Drag the touch up instead action to the First Responder and select the action in the container view controller. In the action to find the indexPath simply loop the visibleCells and check if the sender is isDescendantOfView
:
一个例子是自定义UITableViewCell
. 将 a 添加UIButton
到单元格,但您希望操作上升到具有嵌入 segue 到UITableViewController
. 将 touch up 操作拖动到 First Responder 并在容器视图控制器中选择该操作。在查找 indexPath 的操作中,只需循环可见单元并检查发件人是否为isDescendantOfView
:
- (IBAction)cellButtonTapped:(id)sender{
for(UITableViewCell *cell in self.tableViewController.tableView.visibleCells){
if([sender isDescendantOfView:cell]){
NSIndexPath *indexPath = [self.tableViewController.tableView indexPathForCell:cell];
NSLog(@"tapped %@", indexPath);
}
}
}
Another example could be a reload button: say your first view controller shows an downloaded item with an IBAction
to reload it to get the latest data, then your child controller shows some detail, but you also want them to be able to reload the main item from within the detail, just add a button in the detail and drag its action to First Responder and select the reload IBAction
in the parent controller. This allows you to hook up buttons to parent actions with no additional code like delegate methods!
另一个例子可能是一个重新加载按钮:假设你的第一个视图控制器显示一个下载的项目,IBAction
并重新加载它以获取最新数据,然后你的子控制器显示一些细节,但你也希望他们能够从在详细信息中,只需在详细信息中添加一个按钮并将其操作拖动到 First Responder 并IBAction
在父控制器中选择重新加载。这允许您将按钮连接到父操作,而无需额外的代码(如委托方法)!
For this to work the action needs to be in the responder chain hierarchy or it won't be found, you can read how the chain is built up in the docs. Also note if called from code the view needs to have appeared, viewWillAppear
is too soon.
为此,操作需要位于响应者链层次结构中,否则将无法找到,您可以在文档中阅读链的构建方式。另请注意,如果从代码中调用视图需要出现,viewWillAppear
则为时过早。