ios 按下返回键时如何隐藏键盘 - Swift

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41537721/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 11:01:42  来源:igfitidea点击:

How to hide keyboard when return key is hit - Swift

iosswiftkeyboard

提问by stevetheipad

Trying to hide the iOS keyboard when the return key is hit, but instead it halts and gives me the error seen in the image. Here's the code I'm using:

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

@IBOutlet weak var scoreText: UITextField!

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

enter image description here

enter image description here

回答by Mr. Xcoder

Your problem is that you didn't delegate a textField in order to use that method. First, your class must include the UITextFieldDelegateprotocol:

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

class yourClass: UIViewController, UITextFieldDelegate { ... }

And in the viewDidLoad()method, add this as well:

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

scoreText.delegate = self

And then you have to change this:

然后你必须改变这个:

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

to this:

对此:

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

Final code:

最终代码:

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
    }
}

If this is not working, the problem is not the textFieldShouldReturn()function. Please check your outlet connections.

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

回答by Mochi

Try this

尝试这个

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

回答by Enamul Haque

In swift 4 or 5 , You can use like :

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

  1. create a project
  2. add UITextField & connect to ViewController
  3. implement UITextFieldDelegate to ViewController
  4. Initialize the UITextField delegate
  5. Add textFieldShouldReturn method in ViewController
  6. Here is full code

    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
     }  
    
    }
    
  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
     }  
    
    }