将 TapGestureRecognizer 添加到 UITextView
我想将 *UITapGestureRecognize*r 添加到我的 UITextView 中,因为我想关闭 TextView 所在的“弹出窗口”。所以我想要该方法“当 T*extView* 被点击时,Popup 类的 hide" 被调用。我尝试了如下方法,但由于某种原因它不起作用:
UITapGestureRecognizer *gr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(show)];
[gr setNumberOfTapsRequired:1];
[viewText addGestureRecognizer:gr];
我也不想为其创建子类,因为然后我需要调用“父”方法“隐藏”。
也许您现在可以很好地解决该问题。
先感谢您。
I want to add an *UITapGestureRecognize*r to my UITextView, because I want to close a "Popup" where the TextView is in. So I want, that the method "hide" of the Popup class is called, when the T*extView* is tapped. I tried it like the following, but it isn't working for some reason:
UITapGestureRecognizer *gr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(show)];
[gr setNumberOfTapsRequired:1];
[viewText addGestureRecognizer:gr];
I also don't want to create a Subclass for it, because I then would need to call the "parent"-method "hide".
Maybe you now a good solution for that problem.
Thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不应该使用 UITapGestureRecognizer,而应使用 UITextFieldDelegate。
您可以在这里阅读:
您基本上需要添加
UITextViewDelegate 像这样到你的 .h 文件 -
然后将你的控制器分配为委托:
现在使用委托方法之一,也许:
编辑
好吧,我可以考虑另外 2 种方法:
您可以使用:
祝你好运
You shouldnt use UITapGestureRecognizer but use the UITextFieldDelegate.
You can read about it here:
http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UITextViewDelegate_Protocol/Reference/UITextViewDelegate.html%23//apple_ref/doc/uid/TP40006897
You basicly need to add the
UITextViewDelegate to your .h file like that -
Then assign your controller as the delegate:
Now use one of the delegation methods, maybe:
Edit
Well I can think on 2 additional approaches:
You can use :
Good luck
您是否尝试过 NSLog on show 方法?或者你甚至声明并编写了方法“show”?它应该可以工作,这就是我在文本视图上所做的。
PS 在添加 textview 后不要忘记释放你的手势实例 (gr) :D
Did you try to NSLog on show method? or did you even declare and write method "show" ? It should work and that's how I did on my text view.
P.S don't forget to release your gesture instance (gr) after you add on textview :D
我在实现此功能时也遇到了重大问题,但我遇到了一个愚蠢的问题,即可视化编辑器中的用户交互被关闭。希望这对某人有帮助:)
I had major problems getting this working also but I had one stupid problem, user interaction was turned off in the visual editor. Hope this helps someone :)