Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 5 storyboard, programmatically determine path

I'm having trouble to achieve the following using a storyboard:

When setup is not done: run app -> show settings view controller -> show main navigation controller When setup is done: run app -> show main navigation controller

So basically, I want the app to programmatically start with the settings view in certain cases, and otherwise skip right ahead to the main navigation controller.

I did manage to show the settings view with a modal style segue from the main navigation controller, but I don't know how to display it before the main navigation controller is displayed. Any ideas?

like image 323
René Avatar asked Oct 25 '11 12:10

René


3 Answers

By default, the initial view controller from your main storyboard is instantiated and displayed automatically when your app starts up. To prevent this happening you need to remove the UIMainStoryboardFile setting from your info.plist file.

With no default view controller, you are now free to create one programmatically at app startup. See the UIStoryboard documentation. Use +storyboardWithName:bundle: to load the storyboard and then use –instantiateViewControllerWithIdentifier: to create the correct view controller. You will also need to create a main UIWindow and add the view controller's view to it just like you used to do with .nib based UI. Note that without the UIMainStoryboardFile setting a main window is not created for you - read the explanation.

like image 147
Robin Summerhill Avatar answered Nov 13 '22 05:11

Robin Summerhill


I managed to do it a bit different:

  1. Use a UINavigationController as the initial view controller.
  2. Create a root view controller that will manage the decision of what to load.
  3. Create a Storyboard Segues from the root view controller to the main view and to settings view, and give the segues proper identifiers.
  4. Call the performSegueWithIdentifier with the proper identifier from your root view controller.

Just another solution, hope this helps.

like image 32
amoshaviv Avatar answered Nov 13 '22 04:11

amoshaviv


I did something similar to amoshaviv, his advice is sound. I did it slightly different though, and I'll give some more info. I created a custom MyInitialViewController class, derived from UIViewController, and made this the initial view controller. In the storyboard file, I created modal segues with appropriate names to all (in my case three) possible 'real' first view controllers. In the MyInitialViewController class, I implemented the

- (void)viewDidAppear:(BOOL)animated;

method, to first perform the check which view to switch to, and then do the correct

[self performSegueWithIdentifier:@"NameOfSegue" sender:self];

Effectively, this makes the MyInitialViewController nothing more than a switch performed when it's brought into view. I first tried doing this when loaded because I don't care for actually seeing this view, but that did not work, while viewDidAppear does.

To make this visually smooth, I tried the following. In the properties of the segues, I disabled animation. The view I left empty, and I gave it a background color matching to that of the startup image.

like image 3
ecotax Avatar answered Nov 13 '22 06:11

ecotax