【问题标题】:How to hide keyboard when return key is hit - Swift按下返回键时如何隐藏键盘 - Swift
【发布时间】:2017-05-23 02:33:05
【问题描述】:

当按下返回键时尝试隐藏 iOS 键盘,但它停止并给我图像中看到的错误。这是我正在使用的代码:

@IBOutlet weak var scoreText: UITextField!

func textFieldShouldReturn(_ scoreText: UITextField) -> Bool {
    self.view.endEditing(true)
    return false
}

【问题讨论】:

    标签: ios swift keyboard


    【解决方案1】:

    您的问题是您没有委托 textField 以使用该方法。首先,你的类必须包含UITextFieldDelegate 协议:

    class yourClass: UIViewController, UITextFieldDelegate { ... }
    

    viewDidLoad() 方法中,也添加这个:

    scoreText.delegate = self
    

    然后你必须改变这个:

    func textFieldShouldReturn(_ scoreText: UITextField) -> Bool {
        self.view.endEditing(true)
        return false
    }
    

    到这里:

    func textFieldShouldReturn(_ scoreText: UITextField) -> Bool {
        self.view.endEditing(true)
        return true
    }
    

    最终代码:

    class yourClass: UIViewController, UITextFieldDelegate {
    
        @IBOutlet weak var scoreText: UITextField!
    
        override func viewDidLoad(){
            super.viewDidLoad()
            scoreText.delegate = self
        }
    
        func textFieldShouldReturn(_ scoreText: UITextField) -> Bool {
            self.view.endEditing()
            return true
        }
    }
    

    如果这不起作用,则问题不在于textFieldShouldReturn() 函数。请检查您的插座连接。

    【讨论】:

    • 遇到这个问题:i.imgur.com/PAWVdrb.png 顺便问一下,为什么我需要委托 TextField 而不是标签、按钮等?
    • @stevetheipad 1 - 因为 textFieldShouldReturn 函数包含在 UITextFieldDelegate 中; 2 - 尝试添加您的论点:self.view.endEditing(true),我可能在回答中错过了这一点
    • 添加了 (true) 但仍然显示 Thread 1: signal SIGABRT on the AppDelegate。
    • 那么这里的问题不在于班级!
    • 删除了该文本字段,重新制作,现在可以使用了。诡异的!感谢您的帮助。
    【解决方案2】:

    试试这个

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

    【讨论】:

    • 不走运,同样的问题。
    • @stevetheipad 您是否将视图控制器设置为跟随 UITextField 委托?
    • 抱歉,您是说 IBOutlet 吗?如果是这样,是的,我为 UITextField 添加了其中之一。
    • @stevetheipad 在应用程序委托中崩溃有关文本字段通常意味着您有未连接的挥之不去的插座。检查故事板中的“参考插座”,如果可能的话,向我们展示控制台中的错误,而不仅仅是代码
    【解决方案3】:

    在 swift 4 或 5 中,您可以像这样使用:

    1. 创建项目
    2. 添加 UITextField 并连接到 ViewController
    3. 实现 UITextFieldDelegate 到 ViewController
    4. 初始化 UITextField 委托
    5. 在 ViewController 中添加 textFieldShouldReturn 方法
    6. 这是完整的代码

      class ViewController: UIViewController,UITextFieldDelegate {
      
       //Connect to text field
       @IBOutlet weak var scoreText: UITextField!
      
       override func viewDidLoad() {
         super.viewDidLoad()
      
          //must initialize delegate
          scoreText.delegate = self
        }
      
       //add method
       func textFieldShouldReturn(_ textField: UITextField) -> Bool {
          textField.resignFirstResponder()
          return true
       }  
      
      }
      

    【讨论】:

    • 比别人好,TY
    猜你喜欢
    • 2014-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-08
    • 1970-01-01
    • 2011-04-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多