Working with View Controllers in Storyboards - ios

I'm begin learn iOS and i have a question: when i launch the project, app will load all view controller or just the Initial View Controller in Main.storyboard?
In case, my app have a lots of view controller, e.g 50 VC, i want to check 50 VC be loaded in one time or each VC be loaded when i call e.g like this :
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *vc = [sb instantiateViewControllerWithIdentifier:#"myViewController"];
vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:vc animated:YES completion:NULL];
After research and ask some friends, i see in Apple doc:
The main storyboard is defined in the app’s Information property list
file. If a main storyboard is declared in this file, then when your
app launches, iOS performs the following steps:
It instantiates a window for you.
It loads the main storyboard and instantiates its initial view controller.
It assigns the new view controller to the window’s rootViewController property and then makes the window visible on the
screen.

It just load only Initial View Controller set in Main.storyboard. You can set any view controller as initial view controller from storyboard-> select any view controller -> go to utility portion of xCode-> go to attribute inspector-> now check is Initial View Controller.

From Docs :
A UIStoryboard object encapsulates the view controller graph stored in
an Interface Builder storyboard resource file. This view controller
graph represents the view controllers for all or part of your
application’s user interface. Normally, view controllers in a
storyboard are instantiated and created automatically in response to
actions defined within the storyboard itself. However, you can use a
storyboard object to instantiate the initial view controller in a
storyboard file or instantiate other view controllers that you want to
present programmatically.
StoryBoard will load just initial viewController on load of the app. Other view controllers will load on performing segues or manually by following methods.
- instantiateInitialViewController
- instantiateViewControllerWithIdentifier:
Read more at here

Related

iOS - Pushing to a View Controller that is not in the Storyboard

Ok, first let's name things:
I have a table view in my root view controller wich we'll call "HomeViewController". In that table view each cell tap will push a different view controller. This collection of view controllers we'll call "DetailViewControllers".
I am perfecly aware that I can make this work by simply loading and pushing each of the DetailViewControllers programmatically inside the didSelectRowAtIndexPath method. I want to do something different instead using storyboard and a prototype cell and I'm not succeeding, so I'm looking for some advice since I'm not too well versed in storyboards and segues.
The behavior I'm trying to achieve here is:
I'll have a segue from a prototype cell in the table view in my HomeViewController. That segue pushes a "base" view controller, from which every DetailViewController inherits. These view controllers don't have an assigned view controller in the storyboard either.
In the prepareForSegue method I want to cast the 'destinationViewController' to the 'right' class. The app crashes at that point saying it "Could not cast value of type 'BaseTestViewController' to 'TestViewController'.". Which is totally understandable.
So, my question is: is there a way to make a segue work like that?
You can't create segues programatically. Segues don't exist without a storyboard. You can just push to a view controller via loading a nib file.
Here's how you do it.
Let's say you have a nib file containing the layout of the view for the main view of your view controller. We'll name it "Test" for instance.
Go to that nib file, click file's owner.
Change class to the name of your view controller. In my case, I named it ViewController.
Right click on file's owner. Link the outlet of view of your view controller to the view in the nib file.
Then in the view controller where you want to transition to that view controller, you do it like this:
UIViewController *controller = [[UIViewController alloc] initWithNibName:#"Test" bundle:[NSBundle mainBundle]];
[self presentViewController:controller animated:YES completion:nil];
It will present the view controller modally.
If you are embedding a navigation controller then you can do it like this:
[self.navigationController pushViewController:controller animated:YES];
Hope it helps.
Try like this without storyboard segue.
let objDestVC = self.storyboard.instantiateViewControllerWithIdentifier("DestVCStroyboardId") as! DestinationViewController
self.navigationController?.pushViewController(objDestVC, animated: true)

Present View From Storyboard Programmatically

I am very new to using Storyboards, and am trying to learn. In my app, I want to change from one view to another, modally, without pushing any buttons, i.e. timer runs down then performs the action. I added a notification observer in the main view, and in my storyboard, I added a new view controller, and associated the Custom Class with it called URLWebViewController. In code I wrote out:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle:nil];
URLWebViewController *myVC = (URLWebViewController *)[storyboard instantiateViewControllerWithIdentifier:#"URLWeb"];
[self presentViewController:myVC animated:YES completion:nil];
But I get the warning message
Incompatible pointer types sending 'URLWebViewController' to parameter of type 'UIViewController'
Like I said, I am very new to Storyboards, and here is what it looks like currently:
Your screenshot indicates you have not created a segue between the two view controllers. Do so by click-and-drag from one view controller to the other. In the attribute inspector, give your newly-created segue an identifier, i.e., a string that names it.
To trigger this segue programmatically, in your source view controller (the one you are segueing from) just call performSegueWithIdentifier and pass in the identifier you chose.
It's really simple! I didn't understand the order but let's say you want to pass from ViewController to WebViewController (the name from your storyboard)..Just ctrl-drag from ViewController to WebViewController to create a segue and select modal as the type.
Now click on the line that connects your view controllers and in the right panel give it an identifier. Now in your code just use the method performSegueWithIdentifier using the identifier you wrote.

Move between multiple storyboards by replacing the windows root controller

To move between VCs on a single storyboard, I use the first custom segue from the post :
bidirectional storyboard travel without stacking This replaces the window's root view controller with the destination view controller, so the VCs do not stack and cause memory allocation issues.
I need to use multiple storyboards and so I am after a method of moving to a second storyboard that replaces the windows root controller with the initial VC of the new storyboard (I.e in a similar way to the custom segue I have used throughout the rest of the project.)
The solution should ideally work for IOS6 & IOS7 (the pseudo-segue method has been updated to IOS7 only)
Any ideas?
You can't do this with a segue. Segues can only be made between controllers in the same storyboard. The only way to do this, is in code by instantiating that first controller, and setting it as the window's root view controller.
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"SomeOtherStoryboard" bundle:nil];
NewController *new = [sb instantiateInitialViewController];
self.view.window.rootViewController = new;

how to xib view controller push a storyboard view controller

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.

Shows blank view when loading view controller from StoryBoard

I have a project which is already in App Store.
Now I need to add some more screens using storyboards to the project. The project was previously implemented using nib files.
I have created storyboard for the new view controller which I have added. When the view controller is called it comes up almost blank, none of the views which I have added for the controller are not showing up.
I have added below code how i'm loading the view controller from story board.
-(void)gotoEventsScreen
{
EventsListViewController* eventsScreen = [[UIStoryboard storyboardWithName:#"EventsScreen" bundle:nil] instantiateInitialViewController];
[self.navigationController pushViewController: eventsScreen animated:YES];
}
Note: I have made EventsListViewController as initialviewcontroller and it also has identifier.
I have also tried to load with identifier still it shows me the blank screen.
Actually there was some problem with my project base class view controller which consists the initWithCoder method which was not calling its super class method.

Resources