6

I have implemented what I think is the function from UITextViewDelegate, which deals with URLs being tapped in a UITextView, however the functions now being called in my code.

This is the function I have used from the delegate.

func textView(textView: UITextView!, shouldInteractWithURL URL: NSURL!, inRange characterRange: NSRange) -> Bool {

println("Link Selected!")

}

However I have used breakpoints and the code isn't even being accessed at runtime? Is this the correct function or is there an alternative?

Stephen Fox
  • 14,190
  • 19
  • 48
  • 52

2 Answers2

6

You need to make sure that the class implementing the delegate protocol is set as the delegate for the UITextView.

self.textView.delegate = self
max_
  • 24,076
  • 39
  • 122
  • 211
1

Your class may look something like :

class MyClass: UITextViewDelegate {
     @IBoutet weak var aTextView: UITextView!

      override func viewDidLoad() {
                super.viewDidLoad()
                aTextView.delegate = self 
        }

       // Delegate
       func textView(textView: UITextView, shouldInteractWithURL URL: NSURL, inRange characterRange: NSRange) -> Bool {

                // User click on a link in a textview
                print("Link Selected!")

               // True => User can click on the URL Ling (otherwise return false)
                return true
       }            

}

Sébastien REMY
  • 2,399
  • 21
  • 39