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.