在以编程方式创建的 UITextView 上捕获 UITapGesture
Catch UITapGesture on programmatically created UITextView
我正在以编程方式创建 UITextView
(的子类),并希望在用户点击创建的 UITextView
时显示弹出视图或其他内容。显示弹出视图不是我的问题,但捕捉点击手势是:
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'
错误的原因是 UITapGestureRecognizer
的 target
设置为 tvControl
这意味着 detectTap:
将在 [= 的实例上调用15=].
手势识别器的 target
应设置为 self
:
var tapRecognizer = UITapGestureRecognizer(target:self, action:"detectTap:")
我正在以编程方式创建 UITextView
(的子类),并希望在用户点击创建的 UITextView
时显示弹出视图或其他内容。显示弹出视图不是我的问题,但捕捉点击手势是:
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'
错误的原因是 UITapGestureRecognizer
的 target
设置为 tvControl
这意味着 detectTap:
将在 [= 的实例上调用15=].
手势识别器的 target
应设置为 self
:
var tapRecognizer = UITapGestureRecognizer(target:self, action:"detectTap:")