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.
![Admin](/assets/logo_square_200-5d0d61d6853298bd2a4fe063103715b4daf2819fc21225efa21dfb93e61952ea.png)
Author by
Admin
Updated on July 09, 2022Comments
-
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 over 12 yearsthanks @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 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 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 over 12 yearsThanks~^^ Your answer is very helpful.
-
Jonathan Beebe almost 12 yearsStephen, 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 over 10 yearsIt 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_identifier>"];
-
Alex Nolasco over 10 yearsThis did it! perfect for switching from a login view controller to a navigation controller. Thank you