0

I want to create a segue to from an object (label) inside a tableview cell to another view controller.

example: imagine the instagram feed tableview - tapping the number of likes under an image will push a second view with the list of people who liked an image.

How can i do that? I know it's possible when tapping a cell but i need a solution for when tapping a label inside the cell...

thanks!

jww
  • 97,681
  • 90
  • 411
  • 885
Gil
  • 559
  • 6
  • 18

3 Answers3

1

You have to make the following steps :

  1. Define userInteractionEnabled = true for the label using Interface Builder or in code, as you wish, (VERY IMPORTANT!!) without this the tap is not enabled.
  2. Define an action to the Tap Gesture Recognizer to handle the tap inside the label.
  3. To present the another ViewController you can do one of the following two things :

    • Make a modal segue from the Tap Gesture Recognizer to the another ViewController using the Interface Builder or make an action for the Tap Gesture Recognizer and then present the another ViewController manually using the presentViewController function.

I thought the first is more easy to present the another ViewController, Is up to you.

I hope this help you.

Victor Sigler
  • 23,243
  • 14
  • 88
  • 105
  • Thanks, one more thing - if each cell in the tableview holds data from an object, and the cells get polulated from an array of objects called "cellsArray" how can I find the index in the array that points to the object in the cell with the label I tapped on? Is there a way to get the cell's indexPath when tapping on. An object in the cell? – Gil Mar 16 '15 at 13:36
  • Yes , you can set a 'tag' in each label with the index of the cell in the `cellForRowAtIndexPath` , but you have to define a custom UITableViewCell for the cell and define an outlet inside of the class to you can make then `cell.label.tag = indexPath.row` in the `cellForRowAtIndexPath` and then in the `tapLabel` action you can retrieve the index of the cell – Victor Sigler Mar 16 '15 at 14:58
  • Somehow I cannot get it to work and what i have seen is similar to this post: https://stackoverflow.com/questions/38683564/segue-from-uitableviewcell-by-tapping-on-an-image-inside-of-cell The solution from this post was a bit complicated. Do you mind paste your code and maybe this author and me are both missing something. – An Chin Sep 02 '17 at 23:40
0

You can simply use a button and set its title instead of using a label. If you have to use a UILabel for some reason, then you can do so by setting its userInteractionEnabled property to true and then adding a UITapGestureRecognizer to it.

Cihan Tek
  • 5,349
  • 3
  • 22
  • 29
  • Is there a difference between labels and buttons in terms of appearance? I seem to think so, but I need to take a close look when I get a chance. – Moondra Jul 16 '18 at 00:06
0

You can use UITapGestureRecognizer.

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var lblTxt: UILabel!

override func viewDidLoad() {
        super.viewDidLoad()

        let recognizer = UITapGestureRecognizer(target: self, action:Selector("handleTap:"))
        recognizer.numberOfTapsRequired = 1;
        lblTxt.addGestureRecognizer(recognizer)
        lblTxt.userInteractionEnabled = true;
}

func handleTap(recognizer: UITapGestureRecognizer){
        println("tapped!")
        let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
        let secondViewController = storyBoard.instantiateViewControllerWithIdentifier("secondView") as SecondViewController
        self.presentViewController(secondViewController, animated:true, completion:nil)
    }
Nurdin
  • 23,382
  • 43
  • 130
  • 308