0

I have 2 storyboards. in Main.storyboard I have a view controller with a button. When the button is clicked I load the second storyboard Second.storyboard. This is the code I use to load the Second.storyboard

DispatchQueue.main.async {
    let storyBoard : UIStoryboard = UIStoryboard(name: "Second", bundle:nil)

    let nextViewController = storyBoard.instantiateViewController(withIdentifier: "Tools") as UIViewController
    self.present(nextViewController, animated:true, completion:nil)
}

In Second.storyboard I have a navigation controller and 2 view controller A and B.

The problem is that when running, I don’t see the back button in B to go back to A.

Things that I have tried: I have created another project. I embedded a navigation controller and 2 view controllers A and B in main.storyboard. This works fine. The back button is shown. This tell me that I am missing something when I load the second storyboard

I have tried adding this

// Do any additional setup after loading the view.
self.title = "Title"

let navigationBar = navigationController!.navigationBar
navigationBar.tintColor = UIColor.blue

let leftButton =  UIBarButtonItem(title: "Left Button", style: UIBarButtonItemStyle.plain, target: self, action: nil)
let rightButton = UIBarButtonItem(title: "Right Button", style: UIBarButtonItemStyle.plain, target: self, action: nil)

navigationItem.leftBarButtonItem = leftButton
navigationItem.rightBarButtonItem = rightButton

I see some samples that load storyboards without this line DispatchQueue.main.async {}. However; If I don use it I get an error and the program crush.

How do I add the Navigation Controller? I select the first View Controller, Editor, Embed in, Navigation Controller.

I am trying to display a navigation controller on a second storyboard and it is not showing the back button.

Needles to say that This is the second App I am trying to do and any help would be very much appreciated.

juanjo
  • 3,737
  • 3
  • 39
  • 44

2 Answers2

1

What are you seeing after presenting "Second"? Are you seeing a nav header/title area at all? Just the Back button is missing from it?

  • How are you pushing B from A?

  • I suspect "Tools" is not the Navigation Controller in "Second". When calling storyBoard.instantiateViewController(withIdentifier: "Tools") you want to be referencing the Nav Controller - not A.

Graham Perks
  • 23,007
  • 8
  • 61
  • 83
  • Graham Perks, Thanks for the piece of code. Unfortunately; it doesn't load the next view controller. No errors. It just doesn't load the next View. – Freddy_Laserna Jan 20 '17 at 19:40
  • Ah, sorry, I misunderstood. I've changed my answer. – Graham Perks Jan 20 '17 at 21:57
  • This is how I load the second Storyboard. DispatchQueue.main.async { let storyBoard : UIStoryboard = UIStoryboard(name: "Second", bundle:nil) let nextViewController = storyBoard.instantiateViewController(withIdentifier: "Tools") as UIViewController self.present(nextViewController, animated:true, completion:nil) }. I dont see any title, I don see the back button. – Freddy_Laserna Jan 23 '17 at 15:03
0

Calling presentViewController presents the view controller modally, outside the existing navigation stack; it is not contained by your UINavigationController or any other. If you want your new view controller to have a navigation bar, you have two main options:

Push the new view controller onto your existing navigation stack, rather than presenting it modally:

let VC1 = self.storyboard!.instantiateViewControllerWithIdentifier("MyViewController") as! ViewController self.navigationController!.pushViewController(VC1, animated: true)

I found this answer in this posthttp://stackoverflow.com/questions/25444213/presenting-viewcontroller-with-navigationviewcontroller-swift