【问题标题】:iOS 8 Dismiss keyboard for swiftiOS 8 快速关闭键盘
【发布时间】:2015-10-23 01:09:47
【问题描述】:

我是 swift 的初学者,我一直在使用这个 dismissKeyboard() 方法,但它不适用于键盘扩展。

@IBAction func donePressed (sender: UIButton) {

      dismissKeyboard()


    }

谁能告诉我为什么这不起作用?

谢谢。

编辑:完整代码

导入 UIKit

类键盘视图控制器:UIInputViewController {

var keyboardView: UIView!

@IBOutlet var nextKeyboardButton: UIButton!

override func updateViewConstraints() {
    super.updateViewConstraints()

    // Add custom view sizing constraints here
}

override func viewDidLoad() {
    super.viewDidLoad()

  self.loadInterface()





}


func loadInterface() {

    var keyboardNib = UINib(nibName: "KeyboardView", bundle: nil)

    self.keyboardView = keyboardNib.instantiateWithOwner(self, options: nil)[0] as UIView

    view.addSubview(self.keyboardView)

    view.backgroundColor = self.keyboardView.backgroundColor

    self.nextKeyboardButton.addTarget(self, action: "advanceToNextInputMode", forControlEvents: .TouchUpInside)

    self.nextKeyboardButton.addTarget(self, action: "advanceToNextInputMode", forControlEvents: .TouchUpInside)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated
}

override func textWillChange(textInput: UITextInput) {
    // The app is about to change the document's contents. Perform any preparation here.
}

override func textDidChange(textInput: UITextInput) {
    // The app has just changed the document's contents, the document context has been updated.

}

@IBAction func buttonPressed (sender: UIButton) {

    let title = sender.titleForState(.Normal)

    var proxy = textDocumentProxy as UITextDocumentProxy

    proxy.insertText(title!)

}

@IBAction func spacePressed (sender: UIButton) {

    var proxy = textDocumentProxy as UITextDocumentProxy

    proxy.insertText(" ")

}

@IBAction func deletePressed (sender: UIButton) {

    var proxy = textDocumentProxy as UITextDocumentProxy

    proxy.deleteBackward()

}

@IBAction func donePressed (sender: UIButton) {

resignFirstResponder()


}

}

【问题讨论】:

  • resignFirstResponder() 调用此方法而不是 dismissKeyboard() 但您需要像这样使用 textFieldDemo.resignFirstResponder() 调用...。您的 textField 实例在哪里.....跨度>
  • 没有文本字段,它只是 iphone 的自定义键盘
  • 好的,你只需调用 resignFirstResponder() 并告诉我
  • 我尝试在 safari 和日历中使用它
  • 没用,resignFirstResponder()

标签: ios iphone swift ios8 keyboard


【解决方案1】:

这样试试

self.dismissKeyboard()

【讨论】:

    【解决方案2】:

    也许可以试试:

    view.endEditing(true)
    

    【讨论】:

      【解决方案3】:

      从屏幕上关闭自定义键盘。

      斯威夫特:

      self.dismissKeyboard()
      

      目标-C:

      [self dismissKeyboard];
      

      https://developer.apple.com/documentation/uikit/uiinputviewcontroller/1618196-dismisskeyboard?language=objc

      【讨论】:

        猜你喜欢
        • 2015-01-15
        • 2015-10-19
        • 1970-01-01
        • 1970-01-01
        • 2015-04-01
        • 2011-10-17
        • 2015-12-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多