在编程创建的UITextView上捕获UITapGesture

时间:2015-08-12 06:18:28

标签: swift uitapgesturerecognizer

我以编程方式创建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我的应用程序[7030:481148] - [My_App.CustomTextView detectTap:]:无法识别的选择器发送到实例0x7fe8ba813200   2015-08-12 10:35:13.673我的应用程序[7030:481148] ***由于未捕获的异常终止应用程序' NSInvalidArgumentException',原因:' - [My_App.CustomTextView detectTap:]:无法识别的选择器发送到实例0x7fe8ba813200'

1 个答案:

答案 0 :(得分:2)

错误的原因是target的{​​{1}}设置为UITapGestureRecognizer,这意味着tvControl将在detectTap:的实例上调用}。

手势识别器的CustomTextView应设置为target

self