ipatch
ipatch

Reputation: 4033

What's the proper way to use storyboards View Controllers and scene in iOS application?

I have been working on an iOS application for sometime now, and I think I am not using the View Controllers I have created properly with the storyboard file / scenes I have created for my application.

As it stands, I have two storyboard files, one for the iPhone, and another for the iPad. In the AppDelegate implementation file there is a method called,

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

I have been loading a ViewController from within this method to start the load process for my application.

Since I have two storyboard files now, I sourced some conditional code to load a specific storyboard file based on the device that is running the application.

What is the proper way to load the storyboard file?

After I load the story board file do I initialize the root view controller?

Once my root view controller is loaded should it load the elements specified in the root scene?

If I choose to load another scene from the root scene by clicking on a button in the root scene should I dismiss the root scene, and load the new scene, or is the new scene a subclass of the parent scene? What is the proper way to transition between scenes?

Basically I have a separate view controller for every scene in my application, and I have specified a view controller associated with each scene in IB.

Right now I have a button that can be pressed from the beginning / root scene to load another scene which is associated with another view controller. In this new scene I have a done button which segues back to the root view controller.

If I were click these two buttons repeatedly by clicking on the buttons over and over, am I creating new objects of my view controllers or I am reusing the objects that were already created?

I know this is more than one question but I am trying to get a good grasp on view lifecycles, and how view controllers relate to scenes, and the proper way to load the first view controller, which should load the first scene in an application I presume.

Upvotes: 1

Views: 738

Answers (1)

katzenhut
katzenhut

Reputation: 1742

What is the proper way to load the storyboard file?

Click on your project in the class navigator. It is the uppermost thing you can click in there, and it has the name of your application on it. Some config files will open right where your source code used to be, and you need to find the one where you can specify a storyboard for different devices. I think it is the second tab from the left, but i cannot check that right now. You just specify a storyboard to use for iphones and a storyboard to use for ipads, and youll be fine.

EDIT: you also need to specify an initailView for both storyboard files. This can be done by simply opening the identity inspector (i think) in storyboards with the view in question selected and checking the box next to "is initial View". Try all four inspectors if my memory fails me. It'll be there, i promise ;)

What is the proper way to transition between scenes?

in most cases, you will use a segue. those thigs are new in storyboard, and they are the bomb. Theres a method you can implement called

-(void)prepareForSegue:(UIStoryboardSegue *) sender:(id)sender

where you can check for the identifier of the segue and then manipulate the destinationViewController-properties directly. Heres a good tutorial: http://www.appcoda.com/storyboards-ios-tutorial-pass-data-between-view-controller-with-segue/

If you cant use a segue, however, you will need a rootViewController for anything that displays more than a modal view. Basically, rootViewControllers manage a stack of views, and its not your business how they do it. Depending on your UI you might need a NavigationViewController or a TabBarViewController, just google for both and see what suits you best.

Feel free to ask any questions you might have, ill be glad to help you. Have fun

Upvotes: 1

Related Questions