Ios UILabel上的UICapgestureRecognitor在swift 3中崩溃?
您好,我想在Ios UILabel上的UICapgestureRecognitor在swift 3中崩溃?,ios,objective-c,swift,uitapgesturerecognizer,Ios,Objective C,Swift,Uitapgesturerecognizer,您好,我想在UITableViewCell内的UILabel上添加点击手势。在这里,我实现了这样的代码 override func awakeFromNib() { super.awakeFromNib() let tap = UITapGestureRecognizer(target: self, action: #selector(self.tapFunction)) label.isUserInteractionEnabled = true
UITableViewCell
内的UILabel
上添加点击手势。在这里,我实现了这样的代码
override func awakeFromNib()
{
super.awakeFromNib()
let tap = UITapGestureRecognizer(target: self, action:
#selector(self.tapFunction))
label.isUserInteractionEnabled = true
label.addGestureRecognizer(tap)
}
func tapFunction(sender:UITapGestureRecognizer) {
print("tap working")
}
但它给了我这样的错误
致命错误:在展开可选值时意外发现nil
谁能告诉我这有什么不对吗?这应该是
let tap = UITapGestureRecognizer(target: self, action:#selector(self.tapFunction(_:)))
如果您使用Swift 4,请添加@objc
@objc func tapFunction(sender:UITapGestureRecognizer) {
print("tap working")
}
它现在使用相同的代码工作我刚刚删除了单元格并创建了一个新的UITableviewCell。我猜可能是xcode错误
override func awakeFromNib()
{
super.awakeFromNib()
let tap = UITapGestureRecognizer(target: self, action:
#selector(self.tapFunction))
label.isUserInteractionEnabled = true
label.addGestureRecognizer(tap)
}
func tapFunction(sender:UITapGestureRecognizer) {
print("tap working")
}
因为您的#选择器(self.tapFunction)错误,发送方参数在哪里?@PrashantTukadiya您能告诉我
#选择器(self.tapFunction(:)
而不是#选择器(self.tapFunction)的确切语法是什么吗)
。检查Sh_Khan的答案,并在您的文档中找到@objcmehod@asifsaifi关闭你的问题,因为输入错误不,没关系。我不喜欢用1-2行回答:DNo不需要写一篇文章=DYa,这就是为什么我们提供了评论部分:XDGYZ我使用的是swift 3而不是swift 4。所以我想@objc不会起作用。@PrashantTukadiya评论部分不是用来写答案的。这是要求澄清的问题。所以我认为一行可以回答这个问题,你可以把它写下来作为答案