How does Xcode load the main storyboard?

When I create a new application with one view in Xcode 4.6 using the storyboard, we see that the main function creates a new application using the application delegate, for example:

return UIApplicationMain(argc, argv, nil, NSStringFromClass([MyAppDelegate class])); 

However, if we look at MyAppDelegate.h and MyAppDelegate.m, there is no link anywhere in the code to MainStoryboard.storyboard. This is different from the non-storyboard version, where we can find a line of code that loads the pen file programmatically.

So my question is, how does the storyboard load? (where do I poke to find it?)

+60
ios objective-c xcode
May 22 '13 at 22:26
source share
6 answers

Look at your target settings for the project

enter image description here

Pay attention to the setup of the main storyboard.

If you wanted to do this in code yourself, you would do something like.

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:[NSBundle mainBundle]]; UIViewController *vc = [storyboard instantiateInitialViewController]; // Set root view controller and make windows visible self.window.rootViewController = vc; [self.window makeKeyAndVisible]; return YES; } 
+71
May 22 '13 at 10:37
source share

Have a look at the UIApplicationMain discussion:

Discussion
This function creates an instance of the application object from the main class and creates an instance of the delegate (if any) from this class and sets the delegate for the application. It also sets up the main event loop, including the application launch loop, and starts processing events. If the Application Info.plist file indicates the main nib file to be loaded, including the NSMainNibFile key and a valid nib file name for the value, this function loads this nib file.

When UIApplicationMain is called, a plist file is loaded containing all the information about the application:

enter image description here

What does he “understand” that the xib / storyboard file must be loaded.

+9
May 22 '13 at 22:39
source share

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

 UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; UIViewController* initialView = [storyboard instantiateInitialViewController]; 
+7
May 22 '13 at 10:37
source share

If you want to create an instance using Swift

 var storyboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle()) var vc : AnyObject! = storyboard.instantiateInitialViewController() self.window!.rootViewController = vc as UIViewController self.window!.makeKeyAndVisible() 
+3
Jun 11 '14 at 3:14
source share

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

Main storyboard file base name

In plain text, the UIMainStoryboardFile key UIMainStoryboardFile :

 <key>UIMainStoryboardFile</key> <string>Main</string> 
+2
Aug 22 '16 at 22:52
source share

The bit was late for the party, but you can go 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
Jul 03 '15 at 8:09
source share



All Articles