Search code examples
iosswiftxcodesegueuistoryboardsegue

Segue without StoryBoard


I'm trying to do an app that will have a Home Button on each View, but I'm looking for a way to navigate to Home Screen on click this button, without make a "physical" link for each screen on StoryBoard to home screen.

I'm try to use this code:

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

But this crash on a black screen, some one know how I can do it? Thanks in advance!


Solution

  • You have to instantiate a viewController from storyboard, this way:

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