Home > Net >  UITextView shouldInteractWithURL delegate methods not working through hyperlink
UITextView shouldInteractWithURL delegate methods not working through hyperlink

Time:04-08

I have refered this but not working for me :How to intercept click on link in UITextView?

I have hyperlink text on UITextView, on click of the hyperlinked text we have to open another View Controller.

to create hyperlink I have done this

NSString *str = @"No products found for your pincode,Edit your pincode";
NSMutableAttributedString *aStr = [[NSMutableAttributedString alloc]initWithString:str attributes:nil];
[aStr addAttribute:NSLinkAttributeName value:@"https://www.google.com/" range:[str rangeOfString:@"Edit your pincode"]];
[self.myTextView setAttributedText:aStr];

to open AnotherViewController I used UITextView shouldInteractWithURL delegate method.

- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange interaction:(UITextItemInteraction)interaction{

    TATViewController *tatVC = [[self getStoryboardWithStoryboardType: kListingStoryboard] instantiateViewControllerWithIdentifier: @"TATViewControllerID"];
    [self.navigationController presentViewController: tatVC animated:true completion:nil];
    return false;
}

My question is how to manage TextView delegate code and hyperlink code to open a ViewController.

CodePudding user response:

If the delegate method isn't called, it's usually because of two potential issues:

The delegate method isn't correct. A typo can occurs, and so, the object doesn't implement the delegate method, it implements another method which signature is almost like the delegate one, but not the same. Internally, the object can check if there is a delegate set, and if the delegate implements the method, which it doesn't in this case, and doesn't call it then.

The most encountered error is that the delegate isn't set. You wrote [ _myTextView.delegate self], instead of [_myTextView setDelegate:self] or _myTextView.delegate = self. You were not setting the delegate properly. Instead you were calling the getter/method self on _myTextView, which return _myTextView. You decided to set the delegate through Interface Builder (Storyboard/Xib), but fixing the call would have fix your issue too.

  • Related