我正在使用swift 3.0中的一个项目,我有UIViewController来填充几个文本字段。因此,我使用表格视图作为下拉菜单,一旦选择了一行,它将被分配给UILabel。此外,我添加了一个UITapGuestureRecognizer方法,以确保在点击屏幕后隐藏键盘。不幸的是,由于这种方法,我的didSelectRow无法正常工作,实际上一旦点击一行,该名称将不会被分配给UILable。但是,当我消除UITapGusture方法时,它工作正常。我该如何解决这个问题?代码如下所示。
import UIKit
import CoreData
class AddRecurringExpensesViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var selectCategoryLabel: UILabel!
@IBOutlet weak var expenseNameTextField: UITextField!
@IBOutlet weak var amountTextField: UITextField!
@IBOutlet weak var toTextField: UITextField!
@IBOutlet weak var fromTextField: UITextField!
@IBOutlet weak var recurringexpenseTypeTableView: UITableView!
@IBOutlet weak var fromDatePicker: UIDatePicker!
@IBOutlet weak var fromDatePickerView: UIView!
@IBOutlet weak var toDatePickerView: UIView!
@IBOutlet weak var toDatePicker: UIDatePicker!
var selectCategoryArray = ["Entertainment", "Food", "Membership", "Misc", "Purchase", "Subscription", "Transport", "Utility", "Other"]
override func viewDidLoad() {
super.viewDidLoad()
self.fromDatePickerView.isHidden = true
self.toDatePickerView.isHidden = true
self.recurringexpenseTypeTableView.isHidden = true
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(AddRecurringExpensesViewController.dismissKeyboard))
view.addGestureRecognizer(tap)
}
func dismissKeyboard() {
//Causes the view (or one of its embedded text fields) to resign the first responder status.
view.endEditing(true)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return selectCategoryArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell ()
cell.textLabel?.text = selectCategoryArray[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.selectCategoryLabel.text = selectCategoryArray[indexPath.row]
print("selected row is \(selectCategoryLabel.text)")
self.recurringexpenseTypeTableView.isHidden = true
}
@IBAction func selectCategoryButtonPressed(_ sender: AnyObject) {
self.recurringexpenseTypeTableView.isHidden = false
}
@IBAction func fromDateButtonPressed(_ sender: AnyObject) {
self.fromDatePickerView.isHidden = false
}
@IBAction func fromDateAddedButtonPressed(_ sender: AnyObject) {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd MMM yyyy"
fromTextField.text = dateFormatter.string(from:fromDatePicker.date)
self.fromDatePickerView.isHidden = true
}
@IBAction func toDateButtonPressed(_ sender: AnyObject) {
self.toDatePickerView.isHidden = false
}
@IBAction func toDateAddedButtonPressed(_ sender: AnyObject) {
let dateFormatter = DateFormatter ()
dateFormatter.dateFormat = "dd MMM yyyy"
toTextField.text = dateFormatter.string(from: toDatePicker.date)
self.toDatePickerView.isHidden = true
}
答案 0 :(得分:0)
修改你的消除键盘功能,如下所示,
func dismissKeyboard() {
if self.recurringexpenseTypeTableView.isHidden == true {
view.endEditing(true)
}
}
答案 1 :(得分:0)
你是对的,由于tapGestureRecognizer,DidSelectRow方法将被禁用。 您可以尝试在'touchsbegin'中执行'view.endEditing(true)',记得重写super func。当UITableview存在时,不要使用tapGestureRecognizer。
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
view.endEditing(true)
}
答案 2 :(得分:0)
您可以将它们视为彼此重叠的图层。 使用UITapGuestureRecognizer,您添加了一个新图层。当用户按下屏幕时,将调用顶层(UITapGuestureRecognizer)。它下面的图层不会被调用。
对于这种情况,我建议您删除UITapGuestureRecognizer图层。并在DidSelectRow中添加键盘的消除。