我有一个函数:
@objc func handleLongPress(data: SkelbimasModel, _ gestureRecognizer:UIGestureRecognizer){
if gestureRecognizer.state != .began{
return
}
let touchPoint:CGPoint = gestureRecognizer.location(in: self.Map)
let touchMapCoordinate:CLLocationCoordinate2D =
self.Map.convert(touchPoint, toCoordinateFrom: self.Map)
let annot:MKPointAnnotation = MKPointAnnotation()
annot.coordinate = touchMapCoordinate
self.resetTracking()
self.Map.addAnnotation(annot)
skelbimai.append(annot)
print(skelbimai)
self.Map.removeGestureRecognizer(gestureRecognizer)
Label.isHidden = true
}
我的 SkelbimbasModel 继承了 NSObject :
类 SkelbimasModel:NSObject {...}
而且那个函数没有被调用:
func addLongPressGesture(){
let longPressRecogniser:UILongPressGestureRecognizer = UILongPressGestureRecognizer(target:self , action:#selector(ViewController.handleLongPress(data:_:)))
longPressRecogniser.minimumPressDuration = 0.5 //user needs to press for 1 seconds
self.Map.addGestureRecognizer(longPressRecogniser)
}
当我从函数中删除 SkelbimasModel 属性时,例如:
@objc func handleLongPress(_ gestureRecognizer:UIGestureRecognizer)
工作正常。如何向选择器声明函数,以传递 SkalbimasModel 属性?
最佳答案
选择器签名必须是以下之一。
@IBAction func myActionMethod()
@IBAction func myActionMethod(_ sender: UIGestureRecognizer)
官方文档
A gesture recognizer has one or more target-action pairs associated with it. If there are multiple target-action pairs, they are discrete, and not cumulative. Recognition of a gesture results in the dispatch of an action message to a target for each of the associated pairs. The action methods invoked must conform to one of the following signatures:
@IBAction func myActionMethod()
@IBAction func myActionMethod(_ sender: UIGestureRecognizer)
如果不想区分 View ,可以查看 View 标签。
let tag = gestureRecognizer.view?.tag
关于ios - 为什么我的#selector 不工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47432624/