Selector in swift3

Why does this not work in fast 3? At runtime, it crashes, saying:

'- [my_app_name.displayOtherAppsCtrl tap:]: unrecognized selector sent for example 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") } } 
+47
ios swift3 selector
Jun 26 '16 at 12:14
source share
4 answers

Selector("tap:") should now be written as #selector(tap(gestureReconizer:))

In addition, you must declare tap as func tap(_ gestureRecognizer: UITapGestureRecognizer) according to the new Swift API Guide , and then your selector will become #selector(tap(_:)) .

+116
Jun 26 '16 at 12:26
source share

In Swift 3, it works as follows:

 @IBOutlet var myView: UIView! override func viewDidLoad() { super.viewDidLoad() let tap = UITapGestureRecognizer(target: self, action:#selector(handleTap)) myView.addGestureRecognizer(tap) } func handleTap() { print("tapped") } 
+18
Jan 31 '17 at 1:50
source share

Swift 3 appeared with a new syntax, so instead of using Selector ("tap:"), #selector (tap (gestureReconizer :))

+2
Aug 25 '17 at 19:09 on
source share

Swift 3:

 class MYPTempController: UIViewController { override func viewDidLoad() { super.viewDidLoad() let btn = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 100)) view.addSubview(btn) btn.addTarget(self, action: #selector(MYPTempController.btnClick), for: .touchUpInside) } @objc fileprivate func btnClick() { print("--click--") } } 



 //带参数btn.addTarget(self, action: #selector(MYPTempController.btnClick(_:)), for: .touchUpInside) //监听方法func btnClick(_ sender: UIButton) { print("--click--") } 
0
Jun 01 '17 at 0:56
source share



All Articles