How to set initial storyboard
Asked Answered
D

2

7

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.

Duenna answered 30/7, 2017 at 21:24 Comment(0)
R
16

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
}
Rudie answered 30/7, 2017 at 21:47 Comment(8)
@hotspring Also, you might find the code samples in this question helpful: #10429129Terribly
Could you please illustrate how do you set via code ?Duenna
@AaronBrager, I have seen that answer, in the Admin storyboard board, there is already initial viewcontroller is selected. Why do we still need to assign that viewcontroller as a rootviewcontroller?Duenna
@hotspring because that is how you connect your application's UIWindow to the view controller you initialized from your storyboard. A view controller can be storyboard's initial view controller without being your application's initial view controller.Terribly
@hotspring I have explained elsewhere how to launch an app without using a storyboard. See for example github.com/mattneub/Programming-iOS-Book-Examples/blob/master/…Rudie
@hotspring and see also github.com/mattneub/RegistrationExample/blob/master/… for an example of how to sometimes let the storyboard load the view controller automatically and how to sometimes load it manuallyRudie
Actually there is a way to choose different storyboard based on the version. https://mcmap.net/q/1475857/-how-to-load-multiple-storyboard-files-depending-on-ios-version-5-and-6Walling
@Walling But that isn't what he wants to do (as far as I can tell).Rudie
T
5

You can set your storyboard programmatically like this in the app delegate:

window = UIWindow(frame: UIScreen.main.bounds)
// Or "Admin"
window!.rootViewController = UIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController()! 
window!.makeKeyAndVisible()

You could actually set any view controller. That's how no-storyboard approach is wired up.

Oh, another advantage of doing it in code is it delays the storyboard initialization. If you use the "Main Interface" settings in Xcode, the storyboard is actually initialized BEFORE the application:didFinishLaunchingWithOptions method.

Trainman answered 15/7, 2019 at 16:46 Comment(1)
(unrelated) Thank you. Since adding more Storyboards my app wouldn't boot up due to the window variable being nil. Adding window = UIWindow(frame: UIScreen.main.bounds) fixed this issue.Saintpierre

© 2022 - 2024 — McMap. All rights reserved.