簡體   English   中英

以編程方式在swift中創建視圖之間的segue

[英]Creating segue between views in swift programmatically

目前正在swift中創建一個應用程序,其中所有元素都以編程方式添加。 如何以編程方式為給定按鈕的操作添加segue以從一個視圖切換到另一個視圖?

您可以使用此內部按鈕操作導航到另一個視圖控制器,而不是以編程方式創建segue。

let viewController = MyViewController()
self.navigationController?.pushViewController(viewController, animated: true)

您可以在不使用segue的情況下推送到下一個viewcontroller。

  @IBAction func btnClickNextVC(_ sender: Any) {

        let objSecondVc = self.storyboard?.instantiateViewController(withIdentifier: "ViewController2") as? ViewController2
        self.navigationController?.pushViewController(objSecondVc!, animated: true)
        //or you can use
        present(objSecondVc, animated: true, completion: nil)
    }

注意:不要忘記在故事板中使用navigationcontroller 並且在代碼withidentifier傳遞與viewcontroller相同的storyboard標識符。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM