1
votes

I have a TableViewCell which contains a UITextView. While editing the text in UITextView, another view appears and that dismisses keyboard. After closing that view, I am back to editing UITextView, and focus is on the UITextView; but the keyboard is not there.

For just testing, I have caught a textselection event on that UITExtView, and did resign as first responder and set it again as first responder. Then the keyboard again appeared.

Is there any event that I can catch when the UITExtView gets the focus after closing the other view; so that I can make UITextView resign firstresponder and again set it as first responder.

1
Use Protocol-Delegate for becomeFirstResponder after dismiss other view.Jay Patel
Hi JD, can you please explain your solution ? I did not get it.Rajendra Batta

1 Answers

1
votes

Define Protocol as mention below in UIViewController :

protocol MyTextViewDelegate: class {
    func makeFirstResponder()
}

Then declare protocol in OtherVC and call makeFirstResponder() on dismiss OtherVC :

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

}

Change didSelectRowAt indexPath and write func of MyTextViewDelegate as below :

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

NOTE: Uncheck the "User Interaction Enabled" from Attributes Inspector of UITextView from Storyboard.