簡體   English   中英

沒有StoryBoard的Segue

[英]Segue without StoryBoard

我正在嘗試制作一個在每個視圖上都具有“主頁”按鈕的應用程序,但是我正在尋找一種方法,只需單擊此按鈕即可導航到“主頁”屏幕,而無需為StoryBoard上的每個屏幕建立到主頁的“物理”鏈接屏幕。

我嘗試使用以下代碼:

@IBAction func btnGoInicio(_ sender: Any) {
    let page = MainVC()
    present(page, animated: true, completion: nil)
}

但這崩潰在黑屏上,有人知道我該怎么做? 提前致謝!

您必須使用以下方式從情節提要中實例化viewController:

@IBAction func btnGoInicio(_ sender: Any) {

     let storyboard = UIStoryboard(name: "Main", bundle: nil) // If you have other storyboard instead of Main, use it

     if let page=self.storyboard?.instantiateViewControllerWithIdentifier("YOU_IDENTIFIER") as! MainVC{ 
//You MUST SET THE VIEW CONTROLLER IDENTIFIER "YOU_IDENTIFIER" FROM INSPECTOR INTO STORYBOARD
        self.present(page, animated: true, completion: nil)
    }

}

在代碼中使用segues時,應使用此方法

func performSegue(withIdentifier identifier: String, 
           sender: Any?)

確保在情節提要中設置帶有標識符的腳本,以便以后可以在標識符參數中調用它們。

暫無
暫無

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

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