单击标签时如何显示另一个View控制器

时间:2019-06-07 13:22:09

标签: swift uiviewcontroller uilabel

就像单击按钮一样显示另一个视图控制器,有没有办法用标签来做到这一点?

1 个答案:

答案 0 :(得分:0)

调用下面的功能

注意:请在下面的代码中设置与您相同的标识符

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)
   }
}

第二个ViewController

class SecondViewController: UIViewController {

  override func viewDidLoad() {
      super.viewDidLoad()
  }
}