swift - 单击标签时如何显示另一个 View Controller

标签 swift uiviewcontroller uilabel

就像单击按钮显示另一个 View Controller 一样,有没有办法使用标签来做到这一点?

最佳答案

调用下面的函数

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

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

关于swift - 单击标签时如何显示另一个 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56495194/

相关文章:

swift - 应该使用谁的见证表?

swift - timer scheduled swift - self 标识符错误

ios - 从 self.parentViewController 调用方法什么都不做

ios - 通过子类化 UILabel 以在 Storyboard 上使用的标准文本颜色 - (IBOutlet)

ios - swift : update of view constraints not visible

ios - Twitter profileImageURL 间歇性不加载(iOS Swift)

ios - UIViewController 中的 SKScene

ios - shouldAutorotateToInterfaceOrientation 没有被调用

ios - swift:以编程方式创建 UILabel 固定宽度,根据文本长度垂直调整大小

ios - 调整文本大小以适合时,如何删除 UILabel 末尾的 "..."?