11

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?

Slee
  • 27,498
  • 52
  • 145
  • 243
  • Can you give us an example of what are you trying to do ? what kind of data is responsible for the creation of the new view ? In general i sugest you create a new class or yust a view whit the same class and depending on the user entery you hide or genereate the view content . It'l be easyer for you to – Shawn May 19 '12 at 14:56

2 Answers2

25

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:

  1. selecting the controller in Storyboard
  2. 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.

titusmagnus
  • 2,014
  • 3
  • 23
  • 23
  • perfect, exactly what I was looking for - thanks. Although it is is not totally necessary it is nice to see all of my UIViews on one screen. – Slee May 21 '12 at 11:01
  • Initially it didn't workout, but then.. `[self presentViewController:vc animated:YES completion:NO];` did the magic for me in the last line. – Jay Mayu Nov 21 '13 at 05:23
  • Yes Mayu, it should be noted that using [self.navigationController push...] only works if that view controller is embedded in a navigation view controller. – mihai Jun 19 '14 at 20:55
3

You must assign a Storyboard ID to your view controller.

IB > Show the Identity inspector > Identity > Storyboard ID

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)
}
Community
  • 1
  • 1
SwiftArchitect
  • 47,376
  • 28
  • 140
  • 179