Xcode 4.2 模板更改 - UIApplication 和 UIApplication主窗口.xib
背景:直到 Xcode 4.2,使用任何模板创建的新项目都将包含 MainWindow.xib,因此将 nil 作为 UIApplicationMain() 的第四个参数传递。从 Xcode 4.2 开始,所有模板都通过将类字符串作为第四个参数传递来实例化应用程序委托,并且不在 xib 中构建应用程序的窗口。
在 4.2 中完成此设置很简单,当然它会按预期工作:创建 xib,将 File's Owner 设置为 UIApplication 并连接委托,在 Info.plist 中指定它,main() 中的第四个参数为 nil。
问题:为什么 Apple 现在鼓励实例化应用程序委托并在代码中构建 UIWindow,而不是“旧方式”?有什么好处?
注意事项:如果您选择使用故事板作为管理 UI 的方式,我会期望这种新的模板行为,但如果您取消选中“使用故事板”,我会期望旧的 pass-nil-and-使用-MainWindow.xib 模板。
这个问题是在这里以迂回的方式提出的,但是讨论中的答案有点薄弱。
Background: Up until Xcode 4.2, new projects created using any of the templates would contain a MainWindow.xib and therefore pass nil as the fourth argument of UIApplicationMain(). Starting in Xcode 4.2 all the templates instantiate the application delegate by passing the class string as the fourth argument and do not build the application's window in a xib.
It is trivial to accomplish this setup in 4.2, and of course it works as expected: create xib setting File's Owner to UIApplication and wire up the delegate, specify it in Info.plist, nil fourth argument in main().
Question: Why is Apple encouraging instantiating the application delegate and building the UIWindow in code now instead of the "old way?" What are the benefits?
Considerations: I would expect this new template behavior if you elect to use storyboarding as a way to manage the UI, but if you uncheck "Use Storyboards" I would have expected the old pass-nil-and-use-MainWindow.xib template.
This question was asked in a roundabout way here, but the answers are a little thin on discussion.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你问苹果为什么要做某事?除非苹果公司明确表态,否则不可能有明确的答案,但他们还没有这样做。
就我个人而言,我发现新方法更加优雅、透明且防弹。正如您所说,在旧方法中,主笔尖是由运行时自动加载的,以响应 Info.plist 设置,并且发生的所有其他事情都是通过笔尖完成的,特别是应用程序委托和窗口的实例化以及相关联的连接(应用程序委托必须成为应用程序委托,窗口必须成为应用程序委托的窗口),除了然后我们回到应用程序委托中的代码以最终呈现界面。
这很难理解。我在书中用了大量的语言来描述它。它也很容易破裂。 nib 必须知道应用程序委托类的名称,因此,如果您不喜欢默认创建的那些有趣的长名称,那么当您更改它们时,您很容易搞乱一切。
然而,现在,应用程序委托被简单地命名为 App Delegate,并由 UIApplicationMain() 在代码中实例化,正如您所说的那样;所有其他都也在代码中作为直接后续操作完成:实例化应用程序委托并调用didFinishLaunching,然后我们在代码中创建窗口,将其分配给我们在代码中的属性,加载nib(如果代码中有),在代码中设置窗口的rootViewController,并像以前一样在代码中显示界面。
因此,引导直接暴露给视图,因为它是所有代码。这使得更容易理解和修改而不破坏任何东西。就好像以前模板设计者只是在炫耀有多少东西可以在幕后神奇地自动发生;现在一切都公开、明确地发生。
You're asking why Apple is doing something? There can be no definitive answer, unless Apple has spoken out explicitly, which they have not done.
Personally I find the new approach considerably more elegant, transparent, and bulletproof. As you rightly say, in the old approach the main nib was loaded automatically by the runtime in response to the Info.plist setting, and everything else that happened was done through the nib, in particular the instantiation of the app delegate and the window and the associated wiring (the app delegate must be made the application delegate, the window must be made the app delegate's window), except that then we come back to the code in the app delegate for final presentation of the interface.
This was hard to understand; it took a great deal of verbiage for me to describe it in my book. It was also easy to break. The nib has to know the name of the app delegate class, so if you didn't like those funny long names that were created by default, you could easily mess everything up when you changed them.
Now, however, the app delegate is simply named App Delegate and is instantiated in code by UIApplicationMain(), as you rightly say; and everything else is also done in code as a direct follow-on: the app delegate is instantiated and didFinishLaunching is called, whereupon we create the window in code, assign it to our property in code, load the nib if there is one in code, set the window's rootViewController in code, and show the interface in code as before.
Thus the bootstrapping is directly exposed to view because it's all code. This makes it easier to understand and to modify without breaking anything. It's almost as if previously the template designer was just showing off as to how much stuff could be made to happen magically and automatically behind the scenes; now everything happens out in the open, explicitly.