ios Xcode 如何加载主故事板?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16702631/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How does Xcode load the main storyboard?
提问by agro1986
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 this:
当我使用 storyboard 在 Xcode 4.6 中创建一个新的单视图应用程序时,我们可以看到 main 函数使用应用程序委托创建了一个新应用程序,如下所示:
return UIApplicationMain(argc, argv, nil, NSStringFromClass([MyAppDelegate class]));
However if we look at MyAppDelegate.h and MyAppDelegate.m, there is nowhere in the code that references MainStoryboard.storyboard. This differs from a non-storyboard version where we can find the line of code that loads the nib file programmatically.
然而,如果我们查看 MyAppDelegate.h 和 MyAppDelegate.m,代码中没有任何地方引用 MainStoryboard.storyboard。这与非情节提要版本不同,在非情节提要版本中,我们可以找到以编程方式加载 nib 文件的代码行。
So my question is, how does the storyboard get loaded? (where should I poke to find it?)
所以我的问题是,故事板是如何加载的?(我应该戳哪里才能找到它?)
回答by Chris Wagner
Look at your Target settings for the Project
查看项目的目标设置
Notice the Main Storyboard setting.
注意主故事板设置。
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;
}
回答by Ramy Al Zuhouri
Give a look to the UIApplicationMaindiscussion:
看看UIApplicationMain 的讨论:
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.
讨论
此函数从主体类实例化应用程序对象,并从给定类实例化委托(如果有),并为应用程序设置委托。它还设置主事件循环,包括应用程序的运行循环,并开始处理事件。如果应用程序的 Info.plist 文件通过包含 NSMainNibFile 键和值的有效 nib 文件名指定要加载的主 nib 文件,则此函数加载该 nib 文件。
When UIApplicationMaingets called, a plist file containing all the application info is loaded:
当UIApplicationMain被调用时,会加载一个包含所有应用程序信息的 plist 文件:
That's how it "understands" that the xib/storyboard file needs to get loaded.
这就是它“理解”需要加载 xib/storyboard 文件的方式。
回答by zimmryan
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
它从 info.plist 文件的 UIMainStoryboardFile 设置开始。然后 Xcode 创建一个主窗口,实例化您的第一个视图控制器,并将其添加到窗口中。您仍然可以在类似于 .nib 的代码中使用
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController* initialView = [storyboard instantiateInitialViewController];
回答by pkamb
In Xcode, the human-readable Info.plist section that determines the main storyboard is:
在 Xcode 中,确定主故事板的人类可读的 Info.plist 部分是:
Main storyboard file base name
Main storyboard file base name
In plain text, the key is UIMainStoryboardFile
:
在纯文本中,关键是UIMainStoryboardFile
:
<key>UIMainStoryboardFile</key>
<string>Main</string>
回答by Beanwah
If you want to instantiate with Swift
如果你想用 Swift 实例化
var storyboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
var vc : AnyObject! = storyboard.instantiateInitialViewController()
self.window!.rootViewController = vc as UIViewController
self.window!.makeKeyAndVisible()
回答by kurais
In Xcode 11.3.1, changing Main storyboard file base namealone is not enough, there's a Storyboard Nameconfiguration in the Application Scene Manifestthat should also be changed as well.
在 Xcode 11.3.1 中,仅更改Main storyboard 文件基本名称是不够的,Application Scene Manifest中的Storyboard Name配置也应该更改。
<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>
回答by Tien Dinh
Bit late to the party but you can get to the viewController from the window as shown below
聚会有点晚,但您可以从窗口进入 viewController,如下所示
@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
}
}