0

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.

Sarthak
  • 29
  • 1
  • 6
  • `alertLabel`: That's a `UITextView`, right? Because the name is confusing. What's wrong exactly? Is the delegate method called at all? Did you set the delegate of the `UITextView`? Do you really have a `navigationController`? Because if it's `nil`, nothing will happen. – Larme Apr 07 '22 at 11:43
  • yes that alertLabel is UITextView I edited the name in code..There is no call on delegate method..yes I have set the delegate method in ViewDidLoad: [ _myTextView.delegate self]; .. my code is opening google.com when I click on hyperlink.. – Sarthak Apr 07 '22 at 11:54
  • TextView needs to be selectable and not editable, is that the case? – Larme Apr 07 '22 at 11:59
  • yes made TextView selectable and not editable through storyboard...and I am using that hyperlink code in another function. – Sarthak Apr 07 '22 at 12:06
  • `[ _myTextView.delegate self]`, you meant `_myTextView.delegate = self` or `[_myTextView setDelegate:self]`, right? Could you implement other delegate methods, just to be sure if they are called? Miight need to make the textview editable just to trigger them, like the editing delegate ones. – Larme Apr 07 '22 at 12:10
  • I removed this line [ _myTextView.delegate self] from ViewDidLoad and connected TextView Delegate through storyboard and it worked ...Thanks @Larme – Sarthak Apr 07 '22 at 12:39
  • That's because `[ _myTextView.delegate self]` is not setting the delegate to self! It's either one of my suggestion, but not this one. – Larme Apr 07 '22 at 12:55
  • Is there any possible way that i can use UILabel instead of UItextView in current code ? – Sarthak Apr 08 '22 at 11:36

1 Answers1

1

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.

Larme
  • 24,190
  • 6
  • 51
  • 81