在选择器操作中传递参数
Passing an argument in a selector action
我正在尝试创建一个带有传递参数的动作的长按手势识别器,但是我遇到了这个错误:
Argument of '#selector' does not refer to an '@objc' method, property,
or initializer
到目前为止,我唯一尝试过的是在 removeDate
函数的开头添加 @objc
作为另一个 post 建议,但没有成功。
let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(removeDate(deleteIndex: 3)))
longPressRecognizer.minimumPressDuration = 1.00
cell.addGestureRecognizer(longPressRecognizer)
func removeDate(deleteIndex: Int) {
if deleteIndex != 0 {
dates.remove(at: deleteIndex - 1)
}
}
您不能通过 GestureRecognizer
操作传递任何其他对象,它将允许您传递唯一的 UIGestureRecognizer
对象。如果你想要长按单元格的索引,那么你可以这样尝试。
首先设置UILongPressGestureRecognizer
这样的动作。
let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(removeDate(_:)))
longPressRecognizer.minimumPressDuration = 1.00
cell.addGestureRecognizer(longPressRecognizer)
现在像这样设置 removeDate
动作。
func removeDate(_ gesture: UILongPressGestureRecognizer) {
if gesture.state == .began {
let touchPoint = gesture.location(in: self.tableView)
if let indexPath = self.tableView.indexPathForRow(at: touchPoint) {
print(indexPath)
dates.remove(at: indexPath.row)
self.tableView.reloadData()
}
}
}
我正在尝试创建一个带有传递参数的动作的长按手势识别器,但是我遇到了这个错误:
Argument of '#selector' does not refer to an '@objc' method, property, or initializer
到目前为止,我唯一尝试过的是在 removeDate
函数的开头添加 @objc
作为另一个 post 建议,但没有成功。
let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(removeDate(deleteIndex: 3)))
longPressRecognizer.minimumPressDuration = 1.00
cell.addGestureRecognizer(longPressRecognizer)
func removeDate(deleteIndex: Int) {
if deleteIndex != 0 {
dates.remove(at: deleteIndex - 1)
}
}
您不能通过 GestureRecognizer
操作传递任何其他对象,它将允许您传递唯一的 UIGestureRecognizer
对象。如果你想要长按单元格的索引,那么你可以这样尝试。
首先设置UILongPressGestureRecognizer
这样的动作。
let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(removeDate(_:)))
longPressRecognizer.minimumPressDuration = 1.00
cell.addGestureRecognizer(longPressRecognizer)
现在像这样设置 removeDate
动作。
func removeDate(_ gesture: UILongPressGestureRecognizer) {
if gesture.state == .began {
let touchPoint = gesture.location(in: self.tableView)
if let indexPath = self.tableView.indexPathForRow(at: touchPoint) {
print(indexPath)
dates.remove(at: indexPath.row)
self.tableView.reloadData()
}
}
}