swift 成为FirstResponder

标签 swift keyboard

我需要打开键盘。 当 ViewController 启动时,我使用此函数

func showKeyboardAndFocus(){
    DispatchQueue.main.async {
        self.placeForText.resignFirstResponder()
        self.placeForText.becomeFirstResponder()
    }
}

来自 viewDidLoad 并且它工作正常。 当我关闭它并尝试使用相同的功能打开它时,它不起作用。

为什么?我怎样才能第二次打开它?

最佳答案

显示键盘:

self.inputTextView.becomeFirstResponder()

隐藏键盘:

self.inputTextView.resignFirstResponder()

第二次显示/隐藏

@IBAction func showKeyboardClicked(_ sender: UIButton) {
    textField.becomeFirstResponder()
}

@IBAction func closeKeyboardClicked(_ sender: Any) {
    textField.resignFirstResponder()
}

关于 swift 成为FirstResponder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49089053/

相关文章:

java - Android 模拟器键盘在显示数字键盘时崩溃

ios - 在 ViewControllers cellForItemAt indexPath 方法中设置自定义 CollectionViewCells 标签

android - 窗口管理器添加的覆盖 View 隐藏键盘(Android)

swift - OS X cocoa swift : Setting up a ViewController programmatically (without Storyboard or nib files)

swift - Swift 中 UIView 的圆顶角

javascript - 捕获多个 "onkeydown"并等待 "onkeyup"执行

WPF 入队和重放关键事件

ios - 如何在键盘存在时使 UIView 向上 move

ios - 试图遍历 UIView 和所有 subview 以获得第一响应者,但无法正常工作

ios - 滚动 UITableView 时,UISwitch 多次添加到 UITableViewCell - Swift