【问题标题】:UITextField keyboard not dismissingUITextField 键盘不关闭
【发布时间】:2015-04-29 06:42:38
【问题描述】:

我有一个UIViewController,其中有一个UITextField,当我点击离开或视图被关闭时,我试图关闭键盘。但是,当我拨打resignFirstResponder() 时,键盘仍然没有关闭,我不太清楚为什么。这是我的代码:

class MessageViewController: UIViewController, UITextFieldDelegate {
    var messageTextField : UITextField = UITextField()

    override func viewDidLoad() {
        ...
        messageTextField.frame = CGRectMake(10, UIScreen.mainScreen().bounds.size.height-50, UIScreen.mainScreen().bounds.size.width-80, 40)
    messageTextField.delegate = self
    messageTextField.borderStyle = UITextBorderStyle.Line
    messageTextField.becomeFirstResponder()
    self.view.addSubview(messageTextField)

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHide:", name: UIKeyboardWillHideNotification, object: nil)
        ...
    }

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

    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
        self.view.endEditing(true)
        println("Touched")
    }

    func keyboardWillShow(notification: NSNotification) {
        var keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey]as? NSValue)?.CGRectValue()
        let messageFrame = messageTextField.frame
        let newY = messageFrame.origin.y - keyboardSize!.height
        messageTextField.frame = CGRectMake(messageFrame.origin.x, newY, messageFrame.size.width, messageFrame.size.height)
    }

    func keyboardWillHide(notification: NSNotification) {
        var keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey]as? NSValue)?.CGRectValue()
        let messageFrame = messageTextField.frame
        let newY = messageFrame.origin.y - keyboardSize!.height
        messageTextField.frame = CGRectMake(messageFrame.origin.x, newY, messageFrame.size.width, messageFrame.size.height)
    }

}

有谁知道为什么键盘没有关闭?我以编程方式将 UITextField 添加到视图中,而不是使用情节提要。这有什么不同吗?

【问题讨论】:

  • 使用返回 [textField resignFirstResponder];
  • 使用 TapGesturerecognizer
  • @AshokLondhe 我同时使用了 self.view.endEditing(true) 和 textField.resignFirstResponder()。都没有用。
  • @user1998511 调试您的应用程序并查看委托方法是否被调用..
  • @AshokLondhe 我的委托方法确实被调用了

标签: ios swift uitextfield


【解决方案1】:
 class ViewController: UIViewController,UITextFieldDelegate {

确认协议

messageTextField.delegate=self

设置委托

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

使用此代码..

【讨论】:

  • class ViewController: UIViewController,UITextFieldDelegate 你遵守协议了吗?
  • 除了 UITextFieldDelegate 和设置 messageTextField.delegate = self 之外还有哪些协议?
  • 你能区分这两个对象messageTextFieldtextField。为什么要粘贴提问者已经使用的相同代码。
  • textField 是 textFieldShouldReturn 函数的参数。将其视为您尝试从中“返回”的任何 UITextField 的占位符。另一方面,messageTextField 是项目中 UITextField 的显式变量名称。
【解决方案2】:

以下是我解决该问题的方法。我希望这个方法能解决你的问题。

文本字段

@IBOutlet var userID : UITextField!

功能。

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

在你想要的函数中,你需要编写这个语法。

@IBAction func login(sender: AnyObject)
{
    userID.resignFirstResponder( )
}

这是在 ViewDidLoad 或 ViewDidAppear 中

override func viewDidLoad( )
{
    userID.delegate = self;
}

我无法评论之前的用户。这就是我写这篇文章的原因。 我希望这能给你一些想法。

【讨论】:

    【解决方案3】:
    func textFieldShouldReturn(textField: UITextField) -> Bool {
       self.view.endEditing(true)
       //textField.resignFirstResponder()
       return false
    }
    

    检查delegate 添加到您的viewDidLoad()

    self.yourTextField.delegate = self;
    

    how-to-dismiss-uitextfields-keyboard-in-your-swift-app

    这可能对你有帮助:)

    【讨论】:

      【解决方案4】:

      @vijeesh 的回答可能会奏效,而且逻辑几乎是正确的,但如果您曾经使用多个 UITextField,这在技术上是错误的。 textField 是调用 textFieldShouldReturn 时传递的 UITextField 参数。问题是,你只是在声明:

      textField.resignFirstResponder()
      

      您的程序不知道您指的是什么 UITextField。即使您的程序中可能只有一个 textField,并且您知道它是您的 messageTextField,但程序仍然不知道这一点。它只看到“textField”。所以你必须告诉它为你程序中的每个 UITextField 做什么。即使你只有一个。这应该有效:

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

      【讨论】:

        【解决方案5】:

        非常简单。 首先你添加点击手势

        var tap = UITapGestureRecognizer(target: self, action: "handle:")
        view.addGestureRecognizer(tap)
        

        在函数句柄中

        func handle(tap: UITapGestureRecognizer){
        view.endEditing(true)
        }
        

        所以当您在 UITextField 外部单击时可以关闭键盘

        【讨论】:

          猜你喜欢
          • 2017-03-28
          • 1970-01-01
          • 2011-06-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-12-28
          • 2012-01-12
          • 2016-08-25
          相关资源
          最近更新 更多