문제

I have a UITextView inside a UITableViewCell in a table. "editable" for the UITextView is turned off, which allows me to set dataDetectorTypes to UIDataDetectorTypeAll, which is exactly what I want. The app now detects when the user touches a link in the UITextView, and does the appropriate thing.

The problem arises when the user touches on part of the UITextView where there is no link. I want the didSelectRowAtIndexPath in the UITableView delegate to be called. But it isn't, because the UITextView is trapping the touch, even when no link is detected.

My first guess was to turn userInteractionEnabled on the UITextView to NO. This means that didSelectRowAtIndexPath will get called, but then the UITextView can't detect links. It's a catch-22.

Any ideas on how to fix this?

Thanks for any help.

도움이 되었습니까?

해결책

Maybe you could try passing the touch up the responder chain.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
   [super touchesBegan:touches withEvent:event];
}

다른 팁

Override all four of the UIResponder touch handlers to forward to the text view's superview.

The header file states that "Generally, all responders which do custom touch handling should override all four of these methods. … You must handle cancelled touches to ensure correct behavior in your application. Failure to do so is very likely to lead to incorrect behavior or crashes."

class MyTextView: UITextView {

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        self.superview?.touchesBegan(touches, withEvent: event)
    }

    override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
        self.superview?.touchesMoved(touches, withEvent: event)
    }

    override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
        self.superview?.touchesEnded(touches, withEvent: event)
    }

    override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {
        self.superview?.touchesCancelled(touches, withEvent: event)
    }

}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top