How does Xcode load the main storyboard?

后端 未结 7 1209
遥遥无期
遥遥无期 2020-12-13 08:37

When I create a new single view application in Xcode 4.6 using storyboard, we can see that the main function creates a new application using the application delegate like th

相关标签:
7条回答
  • 2020-12-13 08:38

    Give a look to the UIApplicationMain discussion:

    Discussion
    This function instantiates the application object from the principal class and instantiates the delegate (if any) from the given class and sets the delegate for the application. It also sets up the main event loop, including the application’s run loop, and begins processing events. If the application’s Info.plist file specifies a main nib file to be loaded, by including the NSMainNibFile key and a valid nib file name for the value, this function loads that nib file.

    When UIApplicationMain gets called, a plist file containing all the application info is loaded:

    enter image description here

    That's how it "understands" that the xib/storyboard file needs to get loaded.

    0 讨论(0)
  • 2020-12-13 08:41

    In Xcode 11.3.1, changing Main storyboard file base name alone is not enough, there's a Storyboard Name configuration in the Application Scene Manifest that should also be changed as well.

        <key>UIApplicationSceneManifest</key>
        <dict>
            <key>UIApplicationSupportsMultipleScenes</key>
            <false/>
            <key>UISceneConfigurations</key>
            <dict>
                <key>UIWindowSceneSessionRoleApplication</key>
                <array>
                    <dict>
                        <key>UISceneConfigurationName</key>
                        <string>Default Configuration</string>
                        <key>UISceneDelegateClassName</key>
                        <string>$(PRODUCT_MODULE_NAME).SceneDelegate</string>
                        <key>UISceneStoryboardFile</key>
                        <string>Home</string>
                    </dict>
                </array>
            </dict>
        </dict>
    
    0 讨论(0)
  • 2020-12-13 08:46

    Bit late to the party but you can get to the viewController from the window as shown below

    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate {
    
        var window: UIWindow?
    
        func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    
            var viewController = window?.rootViewController as? ViewController
            if let viewController = viewController {
                // do awesome stuff
            }
    
            return true
        }
    }
    
    0 讨论(0)
  • 2020-12-13 08:51

    If you want to instantiate with Swift

    var storyboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
    var vc : AnyObject! = storyboard.instantiateInitialViewController()
    self.window!.rootViewController = vc as UIViewController
    self.window!.makeKeyAndVisible()
    
    0 讨论(0)
  • 2020-12-13 08:57

    It is started from the UIMainStoryboardFile setting from your info.plist file. Xcode then creates a main window, instantiates your first view controller, and adds it to the window. You can still do this in code similar to the .nib using

    UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    UIViewController* initialView = [storyboard instantiateInitialViewController];
    
    0 讨论(0)
  • 2020-12-13 08:59

    In Xcode, the human-readable Info.plist section that determines the main storyboard is:

    Main storyboard file base name

    In plain text, the key is UIMainStoryboardFile:

    <key>UIMainStoryboardFile</key>
    <string>Main</string>
    
    0 讨论(0)
提交回复
热议问题