ios - 为什么我的#selector 不工作?

标签 ios swift xcode selector

我有一个函数:

 @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/

相关文章:

ios - 用字符串中的空白字符替换 "0"

ios - NSTimer 在 ARC 项目中崩溃

ios - 如何使用 RxSwift 和 RxSwiftDataSources 将 TableView 与代表不同数据类型的多个部分绑定(bind)?

ios - 沿途发送的多点连接和数据

ios - 谷歌地图自定义夜间模式 iOS

ios - 如何获取Xcode构建目录?

objective-c - 通过 View Controller 将委托(delegate)传递给 View Controller

ios - SwiftUI navigationBarBackButtonHidden 没有按预期工作

uitableview - 点击行时启用/禁用按钮

swift - 无法识别的字体系列 'FontAwesome5Free-Solid'