8

I have storyboard in my application, but i don't want to set the Main storyboard file base name in info.plist, as well as I don't want to set the entry point i.e Is Initial View Controller in storyboard for any viewcontroller scene.

Though I want to launch the app with some scene from the storyboard, but all shows me a black screen.

When I tried

let testob:testClass = testClass()
self.window?.rootViewController = testob

in AppDelegate, it didn't worked and got the same black screen.

But when I set the Main storyboard file base name in info.plist, and the entry point i.e Is Initial View Controller in storyboard every thing works.

Is there any solution to do so?

Harshavardhan
  • 1,266
  • 2
  • 14
  • 25
  • Check the following link if it works for you : http://stackoverflow.com/questions/26514028/programatically-setting-root-view-controller-in-swift – Ujjwal Khatri Mar 18 '16 at 10:36

3 Answers3

12

You need to instantiate the window and make it key and visible.

let bounds = UIScreen.main.bounds
self.window = UIWindow(frame: bounds)
self.window?.rootViewController = rootViewController
self.window?.makeKeyAndVisible()
juanelomx
  • 251
  • 3
  • 4
3

You can load a particular view controller from storyboard.

See this answer.

Example:

window?.rootViewController = initialViewController()

where

    private func initialViewController() -> UIViewController {
        if isUserLoggedIn {
            return UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("LoadingViewControllerIdentifier")
        } else {
            return UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("HomeViewControllerIdentifier")
        }
    }
Community
  • 1
  • 1
Sahil Kapoor
  • 11,183
  • 13
  • 64
  • 87
2

After setting self.window?.rootViewController = testob, you have to do the following

[self.window makeKeyAndVisible]

Also, initialize the viewController from storyboard using appropriate method

KrishnaCA
  • 5,615
  • 1
  • 21
  • 31