Juts like clicking a button to show another view contoller, is there a way to do that with a label?
            Asked
            
        
        
            Active
            
        
            Viewed 813 times
        
    0
            
            
        - 
                    add a `UITapGestureRecognizer` – AamirR Jun 07 '19 at 13:23
- 
                    2Possible duplicate of [How to make a UILabel clickable?](https://stackoverflow.com/questions/33658521/how-to-make-a-uilabel-clickable) – AamirR Jun 07 '19 at 13:24
- 
                    where should I add this? how does this work? – Amed LM AB Jun 07 '19 at 13:24
- 
                    Why do you want to do that? Is there any specific reason? – arun siva Jun 07 '19 at 13:32
- 
                    I have a label with fontawesome icons, the icons didn't work with the button (If you know how fontawesome works) so I need to do it with a label. – Amed LM AB Jun 07 '19 at 13:44
- 
                    @AmedLMAB Check this https://stackoverflow.com/a/47176926/8014650 – arun siva Jun 07 '19 at 14:08
1 Answers
0
            
            
        Call below function
NOTE: Please set identifier same which you are you in below code
class firstViewController: UIViewController {
   @IBOutlet weak var yourlabel: UILabel
   override func viewDidLoad() {
       super.viewDidLoad()
       self.addGesture()
   }
   func addGesture() {
       let tap = UITapGestureRecognizer(target: self, action: #selector(self. labelTapped(_:)))
       tap.numberOfTapsRequired = 1
       self.yourlabel.isUserInteractionEnabled = true
       self.yourlabel.addGestureRecognizer(tap)
   }
   @objc
   func labelTapped(_ tap: UITapGestureRecognizer) {
        let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
        let SecondVC = storyboard.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
        self.navigationController?.pushViewController(SecondVC, animated: animated)
   }
}
Second ViewController
class SecondViewController: UIViewController {
  override func viewDidLoad() {
      super.viewDidLoad()
  }
}
 
    
    
        Rohit Makwana
        
- 4,337
- 1
- 21
- 29
- 
                    which code does connect my label to another view controller? what should I write in the action area to do that? – Amed LM AB Jun 07 '19 at 13:42
- 
                    in firstViewController labelTapped() method called when you tap on label(addGesture() method must be implemented). and function will navigate to secondViewController. (NOTE* UIavigationController is must be rootviewcontrooler) – Rohit Makwana Jun 10 '19 at 10:46
