xcode iOS 应用程序中真的需要 MainWindow.xib 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7871241/
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
Is a MainWindow.xib truly needed in iOS application?
提问by 5StringRyan
Before doing any type of iOS 5.0 development using Xcode 4.2, Xcode has provided a "MainWindow.xib" in templates. After downloading and playing with Xcode 4.2 with iOS 5.0, I noticed that none of the templates provided include any "MainWindow.xib" files. Do we need this anymore? I noticed that in the "application didFinishLaunchingWithOptions" method in the App_Delegate that the templates now include code that creates some "UIWindow" and if you have any navigation controllers, it updates the window accordingly.
在使用 Xcode 4.2 进行任何类型的 iOS 5.0 开发之前,Xcode 已经在模板中提供了一个“MainWindow.xib”。在使用 iOS 5.0 下载并使用 Xcode 4.2 后,我注意到提供的模板都没有包含任何“MainWindow.xib”文件。我们还需要这个吗?我注意到在 App_Delegate 的“application didFinishLaunchingWithOptions”方法中,模板现在包含创建一些“UIWindow”的代码,如果你有任何导航控制器,它会相应地更新窗口。
// code that was pulled from a Master Detail application template
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
MasterViewController *masterViewController = [[MasterViewController alloc] initWithNibName:@"MasterViewController" bundle:nil];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:masterViewController];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}
I guess my question is, do I need a "MainWindow.xib" any longer, and if so, then why do the Apple templates exclude them?
我想我的问题是,我是否不再需要“MainWindow.xib”,如果需要,那么为什么 Apple 模板将它们排除在外?
采纳答案by Costique
I think the reason why it's now absent is that creating a window programmatically is much more efficient than opening a xib file and unarchiving the objects. Generally, if you can do it in one line of code, you should do that in code.
我认为它现在不存在的原因是,以编程方式创建窗口比打开 xib 文件和取消归档对象要高效得多。通常,如果您可以在一行代码中完成,则应该在代码中完成。
Read Matthew Gillingham's answer for instructions on how to manually change an old project to work without the MainWindow.xib.
阅读 Matthew Gillingham 的回答,了解如何在没有 MainWindow.xib 的情况下手动更改旧项目以使其正常工作。
回答by Matthew Gillingham
It is has always been an option not to use MainWindow.xib. The UIWindow could be loaded programmatically by the doing the following:
不使用 MainWindow.xib 一直是一个选项。可以通过执行以下操作以编程方式加载 UIWindow:
1) Making sure that the call to UIApplicationMain in main.m looks like this:
1) 确保 main.m 中对 UIApplicationMain 的调用如下所示:
UIApplicationMain(argc, argv, nil, @"MyAppDelegateClassName");
rather than this:
而不是这个:
UIApplicationMain(argc, argv, nil, nil);
2) Making sure that Info.plist does not have a value of "MainWindow.xib" as the main nib file.
2)确保Info.plist没有“MainWindow.xib”作为主nib文件的值。
3) Loading the UIWindow programmatically in the app delegate.
3) 在应用程序委托中以编程方式加载 UIWindow。
So, no, you don't have to use MainWindow.xib and, actually, you never have. You could always do things programmatically.
所以,不,您不必使用 MainWindow.xib,实际上,您从未使用过。你总是可以以编程方式做事。
It is a good question why Apple made this change in the default Xcode settings, and I don't know the answer. But it may have to with performance or the transition to ARC or small application file sizes or something else altogether.
这是一个很好的问题,为什么 Apple 在默认 Xcode 设置中进行此更改,但我不知道答案。但它可能与性能或过渡到 ARC 或小应用程序文件大小或其他完全有关。
回答by Qiulang
I always feel confused about the usage of MainWindow.xib: even with it I still need to set window's rootViewController or addSubview to it. Isn't that information contained in MainWindow.xib ? Why I still need to tell window about that?
我总是对 MainWindow.xib 的用法感到困惑:即使有了它,我仍然需要为其设置窗口的 rootViewController 或 addSubview。该信息不包含在 MainWindow.xib 中吗?为什么我还需要告诉窗口呢?
So removing MainWindow.xib seems to make sense.
所以删除 MainWindow.xib 似乎是有道理的。