Selector en swift3


¿Por qué no funciona esto en swift 3 ? Se bloquea en tiempo de ejecución diciendo:

'-[mi_app_name.displayOtherAppsCtrl tap:]: selector no reconocido enviado a la instancia 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")
}
}
Author: Gherbi Hicham, 2016-06-26

4 answers

Selector("tap:") ahora debe escribirse como #selector(tap(gestureReconizer:))

Además, debe declarar tap como func tap(_ gestureRecognizer: UITapGestureRecognizer) según las nuevas Directrices de la API de Swift en cuyo caso su selector se convertiría en #selector(tap(_:)).

 121
Author: jjatie,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-06-26 12:26:02

En Swift 3 funciona así:

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

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

    myView.addGestureRecognizer(tap)
}

func handleTap() {
    print("tapped")
}
 19
Author: Neen,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-01-31 02:10:07

Swift 3 viene con una nueva sintaxis, por lo que en lugar de usar Selector("tap:"), #selector(tap(gestureReconizer:)) es

 2
Author: Zeeshan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-08-25 19:09:57

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
Author: GeekMeng,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-06-01 00:56:27