2

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!

Lorenzo
  • 3,293
  • 4
  • 29
  • 56
Daniel Beltrami
  • 756
  • 9
  • 22
  • 2
    You probably want to use an unwind segue; they make it easy to get back to a particular view controller no matter how deep your navigation path is. – Paulw11 Sep 26 '18 at 19:16
  • Can we assume that all ViewControllers have the HomeScreen as a previous view controller (even if not immediately previous?) – remingtonchan Sep 26 '18 at 19:16
  • How come you do not want to use a physical link then use a perform segue action when clicked? seems the easiest way to me – Julian Silvestri Sep 26 '18 at 19:22
  • is there a nav bar to put home button in? sucks to put a IBAction in every single view controller. If it must be in view controllers view and not nav bar, I guess you could subclass and define action within button, fire a notification that app delegate gets and wipes out nav stack and inserts home vc at root...thats ugly though – Augie Sep 26 '18 at 19:25
  • @JulianSilvestri, when the application is ready, I will have more than 30 screens, with several links between them, and if I put these links, it will be impossible to identify the navigation – Daniel Beltrami Sep 26 '18 at 19:39
  • See post here:https://stackoverflow.com/questions/36216582/create-and-perform-segue-without-storyboards . Basically not a good idea to achieve segue without storyboard. The storyboard is an integral part of that process – Julian Silvestri Sep 26 '18 at 19:45
  • Essential you DO need to instantiate your view controller to achieve the desired result. However this is probably not possible for you so again you need to use the storyboard – Julian Silvestri Sep 26 '18 at 19:49
  • Thanks @Paulw11 using unwind segue like this https://www.youtube.com/watch?v=ULd2v4mHyQ4 solve my problem – Daniel Beltrami Sep 26 '18 at 19:55

2 Answers2

4

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

}
Andrew21111
  • 868
  • 8
  • 17
0

You should use this method when using segues in your code

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

Make sure you set up your segues with identifiers in the storyboard so they can be called later, in the identifiers parameter.

Nol4635
  • 631
  • 1
  • 14
  • 24