iOS 应用程序中真的需要 MainWindow.xib 吗?

发布于 2024-12-11 07:00:10 字数 1043 浏览 0 评论 0原文

在使用 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;
}

我想我的问题是,我是否还需要“MainWindow.xib”,如果需要,那么为什么 Apple 模板将它们排除在外?

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.

// 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

沐歌 2024-12-18 07:00:10

不使用 MainWindow.xib 一直是一个选择。 UIWindow 可以通过执行以下操作以编程方式加载:

1) 确保 main.m 中对 UIApplicationMain 的调用如下所示:

UIApplicationMain(argc, argv, nil, @"MyAppDelegateClassName");

而不是这样:

UIApplicationMain(argc, argv, nil, nil);

2) 确保 Info.plist 的值不为“MainWindow. xib”作为主 nib 文件。

3) 在应用程序委托中以编程方式加载 UIWindow。

所以,不,您不必使用 MainWindow.xib,实际上,您从来没有使用过。您始终可以通过编程方式执行操作。

这是一个很好的问题,为什么 Apple 在默认 Xcode 设置中进行此更改,但我不知道答案。但这可能与性能或向 ARC 的过渡或较小的应用程序文件大小或其他因素有关。

It is has always been an option not to use MainWindow.xib. The UIWindow could be loaded programmatically by the doing the following:

1) Making sure that the call to UIApplicationMain in main.m looks like this:

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.

3) Loading the UIWindow programmatically in the app delegate.

So, no, you don't have to use MainWindow.xib and, actually, you never have. You could always do things programmatically.

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.

恋你朝朝暮暮 2024-12-18 07:00:10

我认为它现在不存在的原因是,以编程方式创建窗口比打开 xib 文件并取消归档对象要高效得多。一般来说,如果你可以用一行代码完成它,你应该用代码来完成。

阅读 Matthew Gillingham 的回答,了解如何手动更改旧项目以在没有 MainWindow.xib 的情况下工作。

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.

Read Matthew Gillingham's answer for instructions on how to manually change an old project to work without the MainWindow.xib.

苏别ゝ 2024-12-18 07:00:10

我总是对 MainWindow.xib 的使用感到困惑:即使使用它,我仍然需要设置窗口的 rootViewController 或 addSubview 。该信息不是包含在 MainWindow.xib 中吗?为什么我还需要告诉window这件事?

因此删除 MainWindow.xib 似乎是有道理的。

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?

So removing MainWindow.xib seems to make sense.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文