swift3中的选择器 [英] Selector in swift3
本文介绍了swift3中的选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么这不适用于swift 3?它在运行时崩溃说:
Why doesn't this work in swift 3 ? It crashes at runtime saying:
' - [my_app_name.displayOtherAppsCtrl tap:]:无法识别的选择器发送
到实例0x17eceb70'
'-[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")
}
}
推荐答案
选择器(点击:)
现在应写为 #selector(点击(gestureReconizer: ))
此外,你应该声明点击为 func tap(_ gestureRecognizer:UITapGestureRecog nizer)
根据新的 Swift API指南,在这种情况下你的选择器会成为 #selector(tap(_:))
。
Also, you should declare tap as func tap(_ gestureRecognizer: UITapGestureRecognizer)
as per the new Swift API Guidelines in which case your selector would then become #selector(tap(_:))
.
这篇关于swift3中的选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文