iOS TTTAttributedLabel委托didSelectLinkWithURL未被调用
我在项目中设置TTTAttributedLabel时遇到问题。
我在头文件中设置了协议委托
@interface TwitterFeedControlleriPad : UIViewController
我已经将ViewController设置为它(不是nil,我已经检查过了)。
cell.tweetLabel.delegate = self;
它显示标签中的所有链接,但是当我点击它们时,它不会调用该function。
- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url
我之前已经成功使用它,它的代码完全相同! 这让我疯狂!
我知道这不是你的情况,但这适用于所有遇到同样问题的人,并且偶然发现这个问题。
我在与UITapGestureRecognizer
相同的视图上有一个TTTAttributedLabel
。 由于第一个, TTTAttributedLabel
的’ touchEnded
‘function未被调用,它负责处理点击链接。
我通过添加以下行来解决问题: tapGestureRecognizer.cancelsTouchesInView = NO;
解决了! 问题是CollectionViewCell中的“User Interaction Enabled”复选框……它被禁用了! 我花了4个小时才想出来! 不管怎样,谢谢你!
UILabel
userInteractionEnabled
默认情况下似乎已禁用,因此除非您明确启用它,否则它将无效。 那是我的问题。
导致同一问题的一个可能原因(类似于Joeran的回答)是,如果您在视图上有自定义的UITapGestureRecognizer
,则会阻止TTTAttributedLabel
的轻TTTAttributedLabel
手势。 在我的情况下,如果点击链接在一个链接上,我需要阻止点击手势的动作,所以cancelsTouchesInView
是不够的。
我的解决方案:阻止轻击手势被识别。
在viewDidLoad
:
tapGesture.delegate = self
以下是我class级的实际执行情况:
extension MyView: UIGestureRecognizerDelegate { override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool { if gestureRecognizer == self.tapGesture { let view = gestureRecognizer.view let location = gestureRecognizer.location(in: view) let subview = view?.hitTest(location, with: nil) // test if the tap was in a TTTAttributedLabel AND on a link if let ttt = subview as? TTTAttributedLabel, ttt.link(at: gestureRecognizer.location(in: ttt)) != nil { return false } } //else return true } }