【问题标题】:entering text into UI textfield by pressing return button通过按返回按钮在 UI 文本字段中输入文本
【发布时间】:2016-01-25 04:47:18
【问题描述】:

假设我想在文本字段中输入一些数据,例如文本,我想按键盘上的完成或返回按钮将数据输入到 UI 文本字段中。

我该怎么做?

【问题讨论】:

    标签: ios swift core-data uitextfield


    【解决方案1】:

    有一种方法可以在不实现UITextFieldDelegate 的情况下做到这一点。您可以为您的UITextField 添加“目标”,它将在用户返回时调用一个方法。像这样的:

        func init(){
            //Initialize everything else
    
            textField.addTarget(self, action: "textFieldReturned", forControlEvents: UIControlEvents.EditingDidEndOnExit)
        }
    
        //This method will be called after user "returns".
        //Note: Name this method whatever you like, just make sure it's
        //the same name you pass to 'addTarget' method as 'action:' parameter
        func textFieldReturned(){
            textField.resignFirstResponder() //Hides keyboard
    
            //Do everything you need after user presses "Done" button
        }
    

    【讨论】:

      【解决方案2】:

      这应该就像在按下返回键时使用UITextFieldDelegate 处理一样简单。

      因此您可以采用该协议,在您的UITextField 上分配委托,然后在按下返回键时在委托方法textFieldDidEndEditing: 中处理数据。我提到的委托方法可以找到here。确保您已通过从 textFieldShouldReturn: 返回 true 来启用对返回键的处理。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-08
        • 1970-01-01
        • 1970-01-01
        • 2020-12-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多