为什么 Apple 建议以编程方式创建模态导航控制器?

发布于 2024-09-24 09:14:02 字数 121 浏览 7 评论 0原文

“以模态方式呈现导航控制器时,以编程方式创建和配置导航控制器对象通常更简单。虽然您也可以使用 Interface Builder 来执行此操作,但通常不建议这样做。” 事实上,因为我的导航控制器很简单,我宁愿在IB中自定义视图!

"When presenting a navigation controller modally, it is often simpler to create and configure your navigation controller object programmatically. Although you could also use Interface Builder to do so, doing so is generally not recommended."
If fact, because my navigation controller is simple, I would rather customise the view in IB!

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

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

发布评论

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

评论(1

若有似无的小暗淡 2024-10-01 09:14:02

虽然我不知道确切的原因,但这是我的尝试:

以编程方式创建控制器时,您只需要几行代码,事实上,在大多数情况下,这可能真的很少:创建、设置根控制器、呈现和发布。替代方案很快就能枚举出来:

  • 您可以将视图控制器作为底层控制器中的出口。然而,它会一直驻留在内存中。不仅会浪费内存,而且在不需要时也没有太大意义。
  • 您可以通过 nib 文件实例化该控制器。然而,然后你将不得不做很多你无论哪种方式都会做的事情:
    • 使用 alloc,init 在代码中创建控制器
    • 设置一些属性 - 在自定义类、笔尖或几行代码中
    • 展示
    • 释放它

,因为替代方案只需几行代码设置,加载 nib 文件的开销实际上并不是非常便宜,并不值得额外的舒适。如果您正在进行大量设置,那么无论如何,无论是从 nib 加载还是在代码中创建,这都会进入自定义类。

只是我的想法...

While I don't know the exact reason, this is my shot at it:

When creating the controller programmatically, you need only a few lines of code, in fact, in most cases this might be really few: creating, setting the root controller, presenting and releasing. The alternatives are quickly enumerable:

  • You could hold the view controller as an outlet in the underlying controller. Then, however, it would reside in memory all the time. Would not only be waste of memory, but this also doesn't make too much sense while it's not needed.
  • You could instantiate that controller form a nib file. Then, however, you would have to do a lot of things that you'd do either way:
    • Create the controller in code with alloc,init
    • Set up some properties - either in a custom class, the nib or a few lines of code
    • Present it
    • Release it

Now, given that the alternative is only a few lines setup of code, the overhead of loading a nib file, which is in fact not extremely cheap, isn't really worth the additional comfort. If you are doing a lot of setup then this would go into a custom class anyways, no matter whether loading from nib or creating in code.

Just my thoughts...

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