提问者:小点点

swift3中的选择器


为什么这在《雨燕3》中行不通?它会在运行时崩溃,并表示:

“-[MY_APP_NAME.DisplayOtherappSCTRL TAP:]:无法识别的选择器已发送到实例0x17ECEB70”

    override func viewDidLoad() {
    super.viewDidLoad()

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Register cell classes
    //self.collectionView!.register(ImageCell.self, forCellWithReuseIdentifier: reuseIdentifier)

    // Do any additional setup after loading the view.

  let lpgr = UITapGestureRecognizer(target: self, action: Selector("tap:"))
    lpgr.delegate = self
    collectionView?.addGestureRecognizer(lpgr)
}

func tap(gestureReconizer: UITapGestureRecognizer) {
if gestureReconizer.state != UIGestureRecognizerState.ended {
  return
}

let p = gestureReconizer.location(in: self.collectionView)
let indexPath = self.collectionView?.indexPathForItem(at: p)

if let index = indexPath {
  //var cell = self.collectionView?.cellForItem(at: index)
  // do stuff with your cell, for example print the indexPath
  print(index.row)
} else {
  print("Could not find index path")
}
}

共3个答案

匿名用户

选择器(“tap:”)现在应该写成#selector(tap(GestureReconizer:))

此外,您应该按照新的Swift API指南将tap声明为func tap(_gesturerecognizer:UITapGestureRecognizer),在这种情况下,您的选择器将变为#selector(tap(_:))

匿名用户

在Swift3中,它的工作原理如下:

@IBOutlet var myView: UIView!
override func viewDidLoad() {
    super.viewDidLoad()

    let tap = UITapGestureRecognizer(target: self, action:#selector(handleTap))

    myView.addGestureRecognizer(tap)
}

func handleTap() {
    print("tapped")
}

匿名用户

Swift3提供了新的语法,因此不使用Selector(“tap:”),#Selector(tap(gestureReconizer:))是