2

I Have a custom cell that has a UITextView called StatusText. I have an extension that finds all @ signs in the textView and turns it into a link. I want to be able to click that username link and go to the profile view controller.

Here is the following code of the CustomCell:

import UIKit

class profileCell: UITableViewCell, UITextViewDelegate {
    @IBOutlet weak var UserImage: UIImageView!
    @IBOutlet weak var UserName: UILabel!
    @IBOutlet weak var Time: UILabel!
    @IBOutlet weak var Likes: UILabel!

    @IBOutlet weak var Dislikes: UILabel!

    @IBOutlet weak var StatusText: UITextView!

    @IBOutlet weak var LikeButton: UIButton!

    @IBOutlet weak var DislikeButton: UIButton!

      override func awakeFromNib() {
          StatusText.delegate = self
      }

func StatusText(StatusText: UITextView, shouldInteractWithURL URL: NSURL, inRange characterRange: NSRange) -> Bool {
    /* perform your own custom actions here */
    print("firing this right now")

    return true // return true if you still want UIAlertController to pop up
  }
}
Matthew Foster
  • 167
  • 1
  • 10

2 Answers2

0

It sounds like you want to perform a segue into another view controller and pass the username between the two. Check Apple's documentation for information on how to create a segue between two view controllers, and refer to this StackOverflow answer for information on how to pass data from one view controller to the next. Also, when creating methods in a delegate, you need to stub the method name exactly as it is in the protocol, as iOS will not know to call your custom-named method. If you follow option #2 from the last link, your final function will look something like this (edit: fixed now that a working answer has been found):

func textView(_ textView: UITextView, shouldInteractWithURL URL: NSURL,   inRange characterRange: NSRange) -> Bool {
    guard let username = self.getUsernameFrom(URL) else { return false } // I'll let you implement this as an exercise. :)

    let profileVC = ProfileViewController()
    profileVC.username = username

    profileVC.performSegueWithIdentifier("profileSegue", sender: self)

    return true
}
Community
  • 1
  • 1
Kevin Aleman
  • 393
  • 3
  • 9
  • Yeh that's perfect, the problem is that when the link is clicked the function doesn't fire. I tried putting the function in the customCell.swift class but when the link is clicked nothing happens. It is set to be selected. I think the function can't find the link inside the cell. – Matthew Foster Dec 02 '16 at 04:38
  • @MatthewFoster, can you verify that `awakeFromNib` is getting called, and that `StatusText` is instantiated at this time? – Kevin Aleman Dec 02 '16 at 04:45
  • So the awakeFromNib is getting called when the tableView first loads in the view controller. Scrolling up and down the table, when the customCell cell is visible the awakeFromNib does not fire. – Matthew Foster Dec 02 '16 at 05:29
  • @MatthewFoster, interesting. I think this problem is going to be hard to debug without seeing more code. I'm assuming you're using `dequeueReusableCellWithIdentifier` to load each `profileCell`. Why not try to set `StatusText.delegate` to the cell when you're loading it in your `tableView`? – Kevin Aleman Dec 02 '16 at 06:17
  • so what i did was cell.StatusText.delegate = cell which points back to the customCell class. I have a function in the StatusText() function but it still wont fire.. I basically want it like on instagram where you can tap a username in the comments and it goes to that users profile. – Matthew Foster Dec 02 '16 at 06:37
  • Last thing, can you try to change your function declaration to `func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {` and let me know if that fixes the problem? Otherwise, I'll try to code it up and see if I can come up with a solution. – Kevin Aleman Dec 02 '16 at 06:48
0

So instead of making the delegate the customCell I made the viewController the delegate and did what @kgaleman said as follows and it fired.

func textView(_ textView: UITextView, shouldInteractWithURL URL: NSURL,   inRange characterRange: NSRange) -> Bool {
    /* perform your own custom actions here */

    print(URL)

    return true // return true if you still want UIAlertController to pop up

}
Matthew Foster
  • 167
  • 1
  • 10
  • Glad it worked! I updated my answer with the proper method title and an explanation of why it works, so future beginners that stumble upon this question can understand what the fix is and why it works. Cheers! – Kevin Aleman Dec 02 '16 at 17:11