What is the difference between instantiateViewControllerWithIdentifier and initWithNibName, other than former is from storyboard and later is not?
UIStoryboard *signupStory = [UIStoryboard storyboardWithName:#"SignupStory" bundle:[NSBundle mainBundle]];
SignupLoginViewController *signUpVC = [signupStory instantiateViewControllerWithIdentifier:#"SignupVC"];
and
SignupLoginViewController *signUpVC = [[SignupLoginViewController alloc] initWithNibName:#"SignupLoginViewController" bundle:[NSBundle mainBundle]];
If you've created your UI using storyboards, you'll want to call instantiateViewControllerWithIdentifier. Here, the identifier is not part of the view controller itself but is only used by the storyboard to locate the view controller. The storyboard will handle initialization and eventually call initWithCoder, which is why you need to override this when creating subclassed view controllers.
On the other hand, if your UI resides in a pure .xib file - developers generally use initWithNibName. It does, technically, break encapsulation and there are other ways to do it, but you'll see this used most often as it's the designated initializer for the class.
Related
first part of my question is basic understanding.
I believe the other viewControllers in the storyboard are in a "Non instantiated" form when application is launched, and are launched when they are sequed, please confirm?
Second part how do I programmatically instantiate a sibling UIViewController within the storyboard and optionally create a seque to it. I've found some code from Objective-C (pasting below) but looking for a solution in SWIFT.
MyViewController *myVC = [[UIStoryboard storyboardWithName:#"Main" bundle:nil] instantiateViewControllerWithIdentifier:#"MyViewController"];
Your first statement is true to a certain extent. Any controller connected to one that does get instantiated by a relationship segue or an embed segue will also be instantiated (like all the children of a tab bar controller). In your code snippet, you don't need to use storyboardWithName: if you're calling that method from a controller that was created in the same storyboard; you can just use self.storyboard.
var myVC = self.storyboard.instantiateViewControllerWithIdentifier("MyViewController")
I have done a separate project using storyboard.
I need to integrate with an existing big project which doesn't use storyboard.
Is it possible to use storyboard partly (only for some view controllers)? If so how ?
Sure. You can instantiate separate view controllers from storyboard:
Getting a Storyboard Object
+ storyboardWithName:bundle:
Instantiating Storyboard View Controllers
– instantiateInitialViewController
– instantiateViewControllerWithIdentifier:
For ex:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Storyboard" bundle:nil];
YourViewController *yourVC = [storyboard instantiateViewControllerWithIdentifier:#"yourViewControllerIdentifier"];
Yes. It is possible. Any view controllers that aren't on the storyboard are presented in exactly the same way they were in the old projected, by instantiating, setting properties (if need be), and pushing.
I am developing an app, and in the process, I have to arrange view controllers process like storyboard->xib->storyboard because i have to use xib due to AR SDK. Could any friendly guy tell me how to xib->storyboard? Thanks a lot!!
MyViewController* *myViewController =
[[UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle:NULL]
instantiateViewControllerWithIdentifier:#"myController"];
using instantiateViewControllerWithIdentifier or instantiateInitialViewController to get storyboard view controller and you can call transitionFromViewcontroller:toViewController:duration:option:animations:completion api to bring up the view or you can just present view.
I use this tutorial to achieve my custom UITabBarController: link
But I use storyboard instead of xibs. So some of method use this code below:
NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:#"TabBarView" owner:self options:nil];
It is fair for xib but as I think not for storyboard.
This will work for u
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"StoryboardName" bundle:nil];
UIViewController *initViewController = [storyboard instantiateViewControllerWithIdentifier:#"ViewControllerName"];
Dont forget to give identifier to your viewcontroller inside the storyboard.
Note:- Make sure you have given ViewControllerWithIdentifier in storyboard.
I'm afraid storyboards are not well suited for this kind of task. There is no method for getting a specific view from a UIStoryboard instance.
In this case, I would recommend you to create a separate xib file with just the view you want to load on its own and use the code you gave to load it.
On a side note I would also not recommend using a library which has 3 years since the last commit, specially with iOS 7 out which breaks a lot of things.
My iPad App has two storyBoards - FirstStoryBoard and SecondStoryBoard
I have a single ViewController
I notice that Under Project > Targets > Summary there is an option for specifying the MainStoryBoard and if I change this setting the correct story board loads correctly.
The Question - How can I change the behavior so that I can change between the first and second story board at run time. In other words I want to
First, create storyboard instance:
UIStoryboard *board = [UIStoryboard storyboardWithName:#"MyStoryboard" bundle:nil];
and then get first view controller from it:
UIViewController *vc = [board instantiateInitialViewController];
add as child view controller:
[self addChildViewController:vc];
or maybe (if you are in AppDelegate.m / application:didFinishLaunchingWithOptions: method)
self.window.rootViewController = vc;
The Question - How can I change the behavior so that I can change
between the first and second story board at run time.
You can't switch the value of the Main Storyboard setting (specified by the UIMainStoryBoard key in your Info.plist) at runtime, so do the next best thing: use a common storyboard or nib file that contains very little, and then instantiate the storyboard that you want using the UIStoryboard class. For example, you could use a simple nib file to create your app delegate, and have the app delegate's -application:willFinishLaunchingWithOptions: load first view controller from the storyboard of your choice.