通过按回车键触发UITextField展开搜索:为什么/如何工作?

通过按回车键触发UITextField展开搜索:为什么/如何工作?

问题描述:

在我当前的项目中,我发现我可以向UITextField添加展开序列,如果还添加了一个操作didEndOnExit事件的动作,则可以通过按回车键来触发序列.如果didEndOnExit处理程序未与事件连接,则不会触发segue.

In my current project I discovered that I could add an unwind segue to a UITextField and trigger the segue by pressing the return key if I also added an action to handle the didEndOnExit event. If the didEndOnExit handler is not connected to the event then the segue is not triggered.

我想了解这种行为.我的兴趣来自于我可能对segue的运作方式有所了解.另一方面,该行为可能只是UITextFields所独有.

I would like to understand this behavior. My interest stems from that there might be something for me to learn about how segues operate. On the other hand, the behavior might just be unique to UITextFields.

这个简单的GitHub项目演示了该行为.

This simple GitHub project demonstrates the behavior.

感谢您的关注.

您做了一件非常不寻常的事情:您将触发的segue附加到文本字段,作为其动作segue.所以问题很简单:那是什么意思?特别是,什么触发了附加到文本字段的触发segue?文本字段的操作"是什么?

You have done a very unusual thing: you've attached a triggered segue to a text field, as its action segue. So the question is simply: what does that mean? In particular, what triggers a triggered segue attached to a text field? What is the "action" of a text field?

我的意思是,我们知道是什么触发了附加到按钮的触发segue:是用户点击按钮.我们知道是什么触发了附加到表视图单元格的触发segue:是用户选择了单元格.但是文本字段呢?用户必须怎么做才能触发其动作搜索?

I mean, we know what triggers a triggered segue attached to a button: it's that the user taps the button. And we know what triggers a triggered segue attached to a table view cell: it's that the user selects the cell. But what about a text field? What must the user do to trigger its action segue?

放松"业务是一条红鲱鱼,所以让我们从我们的思维中消除它.从第二个视图控制器中删除文本字段,并将其放在 first 视图控制器中.将文本字段的动作序列作为模态序列钩到第二个视图控制器上.

The "unwind" business is a red herring, so let's eliminate it from our thinking. Delete the text field from the second view controller and put it in the first view controller. Hook the text field's action segue as a modal segue to the second view controller.

现在在第一个视图控制器中考虑文本字段.它的行为与您描述的相同:

Now contemplate the text field in the first view controller. It behaves as you have described:

  • 如果我们在文本字段中单击并键入并按回车,则什么也不会发生.

  • If we click in the text field and type and hit return, nothing happens.

如果我们在文本字段中添加"Did End On Exit",然后键入并按回车键,则会关闭键盘并进行搜索.

If we add Did End On Exit to the text field and type and hit return, the keyboard is dismissed and we segue.

好吧,请考虑一下第一个要点中的什么都没有发生"这个短语.键盘未关闭!但是关闭键盘 是文本字段的操作".没有采取任何行动.因此,不会触发动作选择.

Well, think about that phrase "nothing happens" in the first bullet point. The keyboard is not dismissed! But dismissal of the keyboard is the text field's "action". There was no action. So there is no triggering of the action segue.

现在尝试该实验:将视图控制器设为文本字段的委托,并实现此方法:

Now try this experiment: make the view controller the text field's delegate, and implement this method:

extension ViewController : UITextFieldDelegate {
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }
}

在文本字段中键入并单击Return.关闭键盘,然后我们选择到下一个视图控制器.作为响应,请关闭键盘,返回键 是文本字段的操作.

Type in the text field and hit Return. The keyboard is dismissed and we segue to the next view controller. Dismissing the keyboard in response the Return key is the text field's action.

因此,从某种意义上来说,您发现放松的搜索和DidEndOnExit都是红鲱鱼,因为我只是完成了相同的事情而没有一个.重要的是文本字段的操作是什么,以及其响应触发的操作选择.

So you see, in a sense, both the unwind segue and the DidEndOnExit were red herrings, since I just accomplished the same thing without either of those. What matter is what the text field's Action is, and the triggering of its Action Segue in response.