ios - 快速将目标添加到 UITextView 和 UILabel

标签 ios swift

我有一个 UITable,里面有多个标签和 TextView 。我想要在触发 touchupinside 事件时调用一个方法。我使用了 targetForAction 但我无法在其中指定 touchUpInside 事件。并且 textview 不可编辑

总的来说,我希望按钮对 addTarget 的作用由 textview 和标签完成。请帮助我

最佳答案

添加Gesture并尝试

let tap = UITapGestureRecognizer(target: self, action: #selector(self.doubleTapped(_:)))
    tap.numberOfTapsRequired = 1
     yourlabel.tag = indexPath.row
    yourlabel.isUserInteractionEnabled = true
    yourlabel.addGestureRecognizer(tap)

调用方法如

func doubleTapped(_ recognizer: UITapGestureRecognizer) {

        print(recognizer.view!.tag)

}

对于 TextView

添加 yourTextView.delegate = self

func textViewDidChange(_ textView: UITextView) {
    //do continue your work
}

您可以查看其他此类方法here .

关于ios - 快速将目标添加到 UITextView 和 UILabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37461217/

相关文章:

ios - 谷歌驱动器 API : Insufficient Permission iOS

ios - swift : Error when saving to core data context

swift - 使用三元 if 运算符的模式匹配

swift - SpriteKit 游戏中心认证

macos - NSOpenPanel 不允许打开应用程序包

ios - 这看起来像正常的内存增加吗?还是我应该为此担心?

ios - 需要多长时间:将ios应用程序从付费更改为免费

objective-c - 来自同一文件的不同 md5 总和

ios - AVAudioPlayer 播放完毕时切换桌面 View 单元格图像

swift - 在 swift 的条件转场中获取 NSInternalInconsistencyException