load view form StoryBoard that does not have a Segue
-
11-12-2019 - |
Question
I am really liking having all of my view's laid out in the Storyboard but there are times when I will have a view that is shown based on a button that is generated by code so there will be no Segue reference - it will be totally disconnected in the Storyboard. I would still like to design it in the storyboard though so I can have a nice overview of all my screens.
Is it possible for me to load the XIB (or whatever it is in a storyboard) designed in the storyboard when a UIViewControler is loaded?
Solution
I do this in my projects:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *myController = [storyboard instantiateViewControllerWithIdentifier:@"MyViewController"];
[self.navigationController pushViewController: myController animated:YES];
Just make sure you have set your controller's identifier correctly. You can do this by:
- selecting the controller in Storyboard
- in the Utilities panel, click the Attributes inspector. Whatever you decide to put in the Identifier field is what goes in the 'instantiateViewControllerWithIdentifier' statement.
I hope this helps.
OTHER TIPS
You must assign a Storyboard ID to your view controller.
IB > Show the Identity inspector > Identity > Storyboard ID
Swift
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewcontroller = storyboard.instantiateViewControllerWithIdentifier("viewController")
as? UIViewController
if let navigationController = self.navigationController {
navigationController.pushViewController(viewcontroller!, animated: true)
}