ios – swift3中的选择器

为什么这不工作在swift 3?它崩溃在运行时说:

‘-[my_app_name.displayOtherAppsCtrl tap:]: unrecognized selector sent
to instance 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")
}
}
选择器(“tap:”)现在应该写成#selector(tap(gestureReconizer :))

此外,你应该声明点击作为func tap(_ gestureRecognizer:UITapGestureRecognizer)根据新的Swift API Guidelines在这种情况下,你的选择器然后将变为#selector(点击(_ :))。

http://stackoverflow.com/questions/38038418/selector-in-swift3

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:ios – swift3中的选择器