即使隐藏了键盘,UITextView 仍然是第一响应者
UITextView is still first responder even after keyboard is hidden
我有一个包含 UITextView 的 TableViewCell。
在 UITextView 中编辑文本时,会出现另一个视图并关闭键盘。关闭该视图后,我回到编辑 UITextView,焦点在 UITextView 上;但键盘不在那里。
为了进行测试,我在该 UITExtView 上捕获了一个文本选择事件,并辞去第一响应者的身份并再次将其设置为第一响应者。然后键盘又出现了。
在关闭另一个视图后,UIExtView 获得焦点时,是否有任何我可以捕捉到的事件?这样我就可以让 UITextView 退出第一响应者并再次将其设置为第一响应者。
在 UIViewController 中定义如下所述的协议:
protocol MyTextViewDelegate: class {
func makeFirstResponder()
}
然后在 OtherVC 中声明协议并在关闭 OtherVC 时调用 makeFirstResponder() :
class OtherVC: UIViewController {
weak var myTextViewDelegate: MyTextViewDelegate?
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func btnClose(_ sender: UIButton) {
self.myTextViewDelegate?.makeFirstResponder()
self.dismiss(animated: true, completion: nil)
}
}
更改 didSelectRowAt indexPath 并编写 MyTextViewDelegate 的函数如下:
extension ViewController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomCell
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.view.endEditing(true)
let vc = self.storyboard?.instantiateViewController(withIdentifier: "OtherVC") as! OtherVC
self.selectedIndexPath = indexPath
vc.myTextViewDelegate = self
self.present(vc, animated: true)
self.tableView.deselectRow(at: indexPath, animated: true)
}
}
extension ViewController: MyTextViewDelegate {
func makeFirstResponder() {
let cell = tableView.cellForRow(at: self.selectedIndexPath) as! CustomCell
cell.txtView.becomeFirstResponder()
}
}
注意:取消选中 Storyboard 中 UITextView 的属性检查器中的 "User Interaction Enabled"。
我有一个包含 UITextView 的 TableViewCell。 在 UITextView 中编辑文本时,会出现另一个视图并关闭键盘。关闭该视图后,我回到编辑 UITextView,焦点在 UITextView 上;但键盘不在那里。
为了进行测试,我在该 UITExtView 上捕获了一个文本选择事件,并辞去第一响应者的身份并再次将其设置为第一响应者。然后键盘又出现了。
在关闭另一个视图后,UIExtView 获得焦点时,是否有任何我可以捕捉到的事件?这样我就可以让 UITextView 退出第一响应者并再次将其设置为第一响应者。
在 UIViewController 中定义如下所述的协议:
protocol MyTextViewDelegate: class {
func makeFirstResponder()
}
然后在 OtherVC 中声明协议并在关闭 OtherVC 时调用 makeFirstResponder() :
class OtherVC: UIViewController {
weak var myTextViewDelegate: MyTextViewDelegate?
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func btnClose(_ sender: UIButton) {
self.myTextViewDelegate?.makeFirstResponder()
self.dismiss(animated: true, completion: nil)
}
}
更改 didSelectRowAt indexPath 并编写 MyTextViewDelegate 的函数如下:
extension ViewController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomCell
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.view.endEditing(true)
let vc = self.storyboard?.instantiateViewController(withIdentifier: "OtherVC") as! OtherVC
self.selectedIndexPath = indexPath
vc.myTextViewDelegate = self
self.present(vc, animated: true)
self.tableView.deselectRow(at: indexPath, animated: true)
}
}
extension ViewController: MyTextViewDelegate {
func makeFirstResponder() {
let cell = tableView.cellForRow(at: self.selectedIndexPath) as! CustomCell
cell.txtView.becomeFirstResponder()
}
}
注意:取消选中 Storyboard 中 UITextView 的属性检查器中的 "User Interaction Enabled"。