How do I change "initwithNibName" in storyboard?

38,386

Solution 1

The UIStoryboard class is your friend:

UIStoryboard* sb = [UIStoryboard storyboardWithName:@"mystoryboard"
                                              bundle:nil];
UIViewController* vc = [sb instantiateViewControllerWithIdentifier:@"ExampleViewController"];

Solution 2

  • If it is still in its own xib file, then you don't change anything.
  • If you've moved everything into a storyboard, then you wouldn't often need to do this as you'd link between view controllers using segues.

If neither of the above are true, i.e. your view controller is on the storyboard but no segue connects to it, then you want UIStoryboard's instantiateViewControllerWithIdentifier: method described in the documentation. You have to set the identifier in the storyboard for this to work.

Share:
38,386
Admin
Author by

Admin

Updated on July 09, 2022

Comments

  • Admin
    Admin almost 2 years

    I want to change below code with storyboard with Xcode 4.2.

    UIViewController * example     = [[ExampleViewController alloc] initWithNibName:@"ExampleViewController" bundle:nil];
    

    Now ExampleViewController.xib file exist. but I want to make it with storyboard. please help me. (I'm not good at English. Sorry)

  • Admin
    Admin over 12 years
    thanks @jrturton ^^ My view controller is on the storyboard but no segue to it. And change initWithNibName code to 'UIViewController *example = [self.view instantiateViewControllerWithIdentifier:@"ExampleView"];'. But it has error. Is not equal both codes?
  • Caleb
    Caleb over 12 years
    @jokor7 You don't send instantiateViewControllerWithIdentifier: to self.view or any other view, you send it to a storyboard. More specifically, you need to send it to the storyboard that contains the view controller. See the UIStoryboard class, please.
  • jrturton
    jrturton over 12 years
    @jokor7 can't add anything more to Caleb's comment really. With that and stephen's answer you should have everything you need.
  • Admin
    Admin over 12 years
    Thanks~^^ Your answer is very helpful.
  • Jonathan Beebe
    Jonathan Beebe almost 12 years
    Stephen, thank you! I've been searching for quite some time trying to figure out how to instantiate a Storyboard's view. Your answer (and jokor7's question) are like a fresh glass of water in the middle of the dry Sahara Desert.
  • JCoster22
    JCoster22 over 10 years
    It is indeed your friend. If you have only one main storyboard, you don't need to initialize it like above, but can just call: [self.storyboard instantiateViewControllerWithIdentifier:@"<controller_identi‌​fier>"];
  • Alex Nolasco
    Alex Nolasco over 10 years
    This did it! perfect for switching from a login view controller to a navigation controller. Thank you