Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set initial storyboard

I have two storyboards one of them is default called Main and the other one I have just added is called Admin.

Main is used for customer, Admin is used for owner of the app.

I wonder how do you set initial/main interface storyboard programmatically.

enter image description here

P.S. I know how to change it via xcode but do not know programatically.

like image 393
casillas Avatar asked Jul 30 '17 21:07

casillas


1 Answers

You do not set an initial storyboard programmatically.

Here's how it works. Either you have a main storyboard listed in Xcode under Main Interface or you don't:

  • If you do, that is the initial storyboard, period. That storyboard is loaded automatically at launch time, and its initial view controller becomes the window's root view controller (and the interface is then displayed automatically).

  • If you don't (that is, if the Main Interface field is empty), then nothing happens automatically. It is up to your code to obtain a view controller, somehow (possibly from a storyboard), and make its the window's root view controller (and to show the interface).

So, to sum up, either everything happens automatically or nothing happens automatically. There is no intermediate state, as you seem to imagine, in which you can programmatically change things so that a different storyboard is loaded automatically.

There is, however, a kind of intermediate state where you permit the Main Interface storyboard to load but then you ignore it. In your implementation of application:didFinishLoading..., you would then sometimes do the thing I said in the second bullet point, i.e. load a different view controller and make it the root view controller. This works because the automatic stuff I mentioned in the first bullet point has already happened by the time application:didFinishLoading... is called. So, in effect, your code sometimes permits that automatic stuff and sometimes overrides it.

In this example from my own code, we either use the Main Interface storyboard to load the initial view controller or we load a different view controller from the storyboard ourselves, depending on a value in User Defaults:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    if let rvc = self.window?.rootViewController {
        if NSUserDefaults.standardUserDefaults().objectForKey("username") as? String != nil {
            self.window!.rootViewController = rvc.storyboard!.instantiateViewControllerWithIdentifier("root")
        }
    }
    return true
}
like image 181
matt Avatar answered Nov 07 '22 14:11

matt