0

Juts like clicking a button to show another view contoller, is there a way to do that with a label?

Amed LM AB
  • 43
  • 7

1 Answers1

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