1

I have created an application in which has two sets of ViewControllers/Storyboards. I would like to go from the orginal ViewController/Storyboard and then load another ViewController/Storyboard after pressing a button. I have tried different things such as this answer but can not figure this out.

Here is the code for the first ViewController:

import UIKit

class HomeController: UIViewController {

@IBAction func enterCottageBuddyApp(_ sender: Any) {
    let storyBoard : UIStoryboard = UIStoryboard(name: "Home", bundle:nil)

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

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
}


}

And here is the storyboard:

enter image description here

This was the Main.storyboard but I have decided to rename it to Home.storyboard (I Have updated the info.plist to reflect this change and this storyboard loads properly).

Here is the separate ViewController (UITabBarController) I created myself:

import UIKit

class CottageTabsController: UITabBarController {

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}

}

With its Storyboard I added myself as well for the tab bar controller:

enter image description here

I have added these in separate files for a more clean file hierarchy (such as having a storyboard and viewcontroller for the landing page as well as a new storyboard and tabbarcontroller for once a user enters the application). When I click the "Enter App" button on the Home storyboard the app crashes with the error: 'NSInvalidArgumentException', reason: 'Storyboard (<UIStoryboard: 0x600002901f80>) doesn't contain a view controller with identifier 'CottageTabsView''

I made sure to add the Storyboard ID and Restoration ID in the inspector and include them in the action method for the button but I can not seem to make it work no matter what I try. I would like the app to go from the Home.storyboard and HomeController to loading the CottageTabsController and CottageTabs.storyboard.

Thanks!

PhillyD
  • 181
  • 1
  • 18
  • Can you show a screenshot of the project navigator? – aheze Oct 16 '20 at 18:18
  • Also, are you still using `HomeController`? I don't see it in the storyboard – aheze Oct 16 '20 at 18:20
  • @aheze I have added it, and once I click the button on the `HomeController` I would like to use the `CottageTabBarsController` instead, like navigate from one to the other. Sorry if my terms are incorrect I am new to iOS and trying to get better with it! – PhillyD Oct 16 '20 at 18:21
  • Np. Try replacing `Home` with `CottageTabs`, because you moved `CottageTabsController` into its own storyboard. See my answer below – aheze Oct 16 '20 at 18:26

1 Answers1

1

CottageTabsController is in its own storyboard, so you need to to change replace UIStoryboard(name: "Home", bundle: nil) with UIStoryboard(name: "CottageTabs", bundle: nil).

@IBAction func enterCottageBuddyApp(_ sender: Any) {
    let storyBoard : UIStoryboard = UIStoryboard(name: "CottageTabs", bundle:nil) /// <- Different storyboard!

    let nextViewController = storyBoard.instantiateViewController(withIdentifier: "CottageTabsView") as!
    CottageTabsController
    self.present(nextViewController, animated:true, completion:nil)
}
aheze
  • 24,434
  • 8
  • 68
  • 125
  • This was the issue, using a new `ViewController` I now know I have to instantiate that storyboard. Another question, when I click the button I still see in the debugger that the old `ViewController` is still there in the view hierarchy and its visible to the user. Is there a way to load the new `ViewController` and only have that one exist? – PhillyD Oct 16 '20 at 18:34
  • You could check out this [answer](https://stackoverflow.com/a/42339949/14351818), but usually you can just keep the old one. – aheze Oct 16 '20 at 18:38
  • 1
    I actually used the `fullscreen` option for the `modalPresentationStyle` on the next controller. In the hierarchy debugger it seems as though the HomeController is no longer there so the resources are saved there without discarding it! Thanks again! – PhillyD Oct 16 '20 at 18:58