gpt4 book ai didi

swift - 在以编程方式创建的 UITextView 上捕获 UITapGesture

转载 作者:搜寻专家 更新时间:2023-11-01 07:32:04 27 4
gpt4 key购买 nike

我正在以编程方式创建 UITextView(的子类),并希望在用户点击创建的 UITextView 时显示弹出 View 或其他内容。显示弹出 View 不是我的问题,但捕捉点击手势是:

func createCustomText(text:String){
var tvControl = CustomTextView(frame: CGRectMake(CGFloat(Int.random(50...100)),
CGFloat(Int.random(50...100)), 100, 30))
tvControl.text = text
baseView.addSubview(tvControl)
tvControl.font = UIFont(name: "BigruixianthinGB1.0", size: 12)
tvControl.backgroundColor = UIColor.clearColor()
activeTextView = tvControl
var tapRecognizer = UITapGestureRecognizer(target:tvControl, action:"detectTap:")
tvControl.gestureRecognizers = [tapRecognizer]
}

func detectTap(recognizer: UITapGestureRecognizer) {
println("tap tap")
}

但它会导致错误:

2015-08-12 10:35:13.665 My App[7030:481148] -[My_App.CustomTextView detectTap:]: unrecognized selector sent to instance 0x7fe8ba813200 2015-08-12 10:35:13.673 My App[7030:481148] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[My_App.CustomTextView detectTap:]: unrecognized selector sent to instance 0x7fe8ba813200'

最佳答案

错误原因是 UITapGestureRecognizertarget 设置为 tvControldetectTap: 将在 CustomTextView 的实例上调用。

手势识别器的target应该设置为self:

var tapRecognizer = UITapGestureRecognizer(target:self, action:"detectTap:")

关于swift - 在以编程方式创建的 UITextView 上捕获 UITapGesture,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31957375/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com