I am following an iOS dev tutorial and reading their code. First Code:
override func viewDidLoad() {
super.viewDidLoad()
let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(hideKeyboard))
gestureRecognizer.cancelsTouchesInView = false
tableView.addGestureRecognizer(gestureRecognizer)
}
Second Code:
@objc func hideKeyboard(_ gestureRecognizer: UIGestureRecognizer) {
let point = gestureRecognizer.location(in: tableView)
let indexPath = tableView.indexPathForRow(at: point)
if indexPath != nil && indexPath!.section == 0 && indexPath!.row == 0 {
return
}
descriptionTextView.resignFirstResponder()
}
From what I understand, the first code enables keyboard disappeared when user tap anywhere on the screen. To implement this, Target-Action pattern is hired.
In the first code, #selector(hideKeyboard)
, hideKeyboard doesn't seems to have any parameter. So why there is a parameter in the helper function (second code).
If the parameter is valid, why is gestureRecognizer: UIGestureRecognizer
?
I don't quite understand how does the parameter passing work?