0

I am developing an iOS app and using storyboards. In my storyboard, I had set a view controller as the initial view controller. Everything working fine.

Now I have to write some login in app delegate to decide which view controller to show at the beginning because this depends on how far the user is in the login process.

So, I removed the initial view controller mark from my storyboard and removed the storyboard setting from my plist file.

Now, in the app delegate I have this code -

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
UIViewController *initialViewController = [storyboard instantiateViewControllerWithIdentifier:[XYZUtils getStartScreenViewController]];
XYZStartScreenViewController *startScreenViewController = (XYZStartScreenViewController *)initialViewController;

[self.window addSubview:startScreenViewController.view];
[self.window setRootViewController:startScreenViewController];

[self.window setBackgroundColor:[UIColor whiteColor]];
[self.window makeKeyAndVisible];

This does not display the view controller - I am just getting a black screen and no error messages. On using breakpoints to walk through the above code, there is no error. The view controller is being instantiated but it is not getting displayed.

Am I missing something here?

Siddharth
  • 5,009
  • 11
  • 49
  • 71
  • Do not call `[self.window addSubview:startScreenViewController.view];` – rmaddy Jan 19 '14 at 19:04
  • @rmaddy Yes, I tried that as well but again the same problem – Siddharth Jan 19 '14 at 19:07
  • I didn't say it would solve the problem. :) But setting the root view controller is enough. Don't also add it as a subview. – rmaddy Jan 19 '14 at 19:10
  • Understood. I believe that `self.window` is `nil` and hence the problem. If not using storyboarding, then `self.window` is not instantiated automatically. Have to manually create a main `UIWindow`. Not sure, though, how to do that! – Siddharth Jan 19 '14 at 19:13

2 Answers2

1

Read this post.. It is better you make an empty view controller and mark that as initial view controller And do all login process in that view controller's viewDidLoad method.

Linking a new viewcontroller to Storyboard?

Community
  • 1
  • 1
Taseen
  • 1,213
  • 1
  • 11
  • 18
0

For some reason, when you deselect the initial view controller setting in the storyboard, then your app will not get a UIWindow setup in didFinishLaunching.

So, what you should so is instantiate your own window there; just add this to the beginning of your application didFinishLaunchingWithOptions method:

UIWindow* window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

I think this solution is perfectly fine, but a "cleaner" one since you are using the storyboard, is having some sort of "LoginManagerViewController" as your initial view controller with the responsibility of handling where to do next based on how far the user is in the login process.

sergio
  • 68,819
  • 11
  • 102
  • 123