xcode 故事板是否消除了对 .nib 的需要

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8495179/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:44:23  来源:igfitidea点击:

Does storyboard eliminate the need for .nib

iphonexcodecocoa-touchuistoryboard

提问by William Sham

With the introduction of storyboard, I wouldn't select to create a .xib/.nib when I create a subclass of UIViewController, because I can just drag out a viewcontroller in interface builder and assign it to the new class.

随着storyboard的引入,我在创建UIViewController的子类时不会选择创建.xib/.nib,因为我可以在界面构建器中拖出一个viewcontroller并将其分配给新类。

So, with storyboard, when would i need to use the .xib/.nib file?

那么,对于故事板,我什么时候需要使用 .xib/.nib 文件?

Thank you.

谢谢你。

采纳答案by Akki

I guess, storyboard contains the .xib/.nib files for all your views. It present relationships between them and helps you to avoid confusion if you have a LOT of views. Also it saves your time and nerves when writing code.

我猜,故事板包含所有视图的 .xib/.nib 文件。它展示了它们之间的关系,并在您有很多观点时帮助您避免混淆。它还可以节省您在编写代码时的时间和精力。

回答by Tim

Storyboards don't completely remove the need for NIB files. One common example is when creating subviews to go inside a UIScrollView. You can't create the subviews separately in a storyboard; instead you have to create separate NIB(s) for the subviews and attach them to the scroll view programatically.

故事板并没有完全消除对 NIB 文件的需要。一个常见的例子是当创建子视图进入UIScrollView. 您不能在故事板中单独创建子视图;相反,您必须为子视图创建单独的 NIB,并以编程方式将它们附加到滚动视图。

In fact, on almost any occasion where you have a need for subviews which change at runtime you'll want to use separate NIBs.

事实上,几乎在任何需要在运行时更改的子视图的情况下,您都希望使用单独的 NIB。

Storyboards are great for certain kinds of apps, but for anything even remotely complicated the old way works very well still. An additional benefit is that they co-exist nicely. You can put placeholder views in your storyboard and fill them programatically with NIB-defined views later on.

故事板非常适合某些类型的应用程序,但对于任何稍微复杂的东西,旧的方式仍然非常有效。另一个好处是它们可以很好地共存。您可以将占位符视图放在故事板中,并在稍后使用 NIB 定义的视图以编程方式填充它们。

回答by Besi

I have tried out storyboarding lately and I do have mixed feelings about its use:

我最近尝试过故事板,但我对它的使用感觉很复杂:

Pro's

  • Convenience: You might have to write much less code. So you can for example show a static view without having to write any code.
  • Overview: You can quickly see how the ViewControllers relate to each other
  • Efficiency: For the simple use cases I find the Storyboarding much more efficient than "the old way".

    Example 1: Editor > Embed In > Navigation Controllerand voilà, no more instantiating and configuring is required.

    Example 2: You can make "Prototype Cells" for TableView which speeds up the creation of static table views dramatically. AFAIK this is not possiblewith nib files

  • It's cool :-)
  • 方便:您可能需要编写更少的代码。例如,您可以显示静态视图而无需编写任何代码。
  • 概述:您可以快速了解 ViewController 之间的关系
  • 效率:对于简单的用例,我发现故事板比“旧方式”更有效。

    示例 1:Editor > Embed In > Navigation Controller瞧,不再需要实例化和配置。

    示例 2:您可以为 TableView 制作“原型单元格”,这显着加快了静态表格视图的创建。AFAIK 这对于 nib 文件是不可能的

  • 这个很酷 :-)

Con's

骗子

  • I find that the re-usability somehow suffers with storyboards. Although I can segue to a given ViewController from multiple points, it is still limited since in one case I might use the VC as a Popover and in another as a Master-ViewController.
  • Assuming that you are working in a big team and different people are changing different parts of your application, having one monolithic storyboard which you then have to merge might pose a big problem team-work-wise.
  • I really would like to have some mechanism to include or reference existing nib files into the storyboard which is currently not possible.
  • 我发现可重用性在某种程度上受到故事板的影响。尽管我可以从多个点转到给定的 ViewController,但它仍然受到限制,因为在一种情况下我可能将 VC 用作 Popover,而在另一种情况下用作 Master-ViewController。
  • 假设您在一个大团队中工作并且不同的人正在更改应用程序的不同部分,那么拥有一个然后必须合并的整体故事板可能会在团队合作方面带来大问题。
  • 我真的很想有一些机制将现有的 nib 文件包含或引用到故事板中,这是目前不可能的。

So considering these points I still find storyboarding very appealing but it makes sense to combine both approaches which is not really a big deal. Additionally, this technology it is still quite new so it might improve in the (near) future.

所以考虑到这些点,我仍然觉得故事板非常吸引人,但将这两种方法结合起来是有道理的,这并不是什么大问题。此外,这项技术仍然很新,因此它可能会在(不久的)将来得到改进。

回答by Caleb

If you use storyboards, you generally won't need to create separate .xib files. You canstill use a .xib file if you want to -- it might be useful e.g. if you have a particularly complex view that you'd like to configure by itself. Most of the time, though, there won't be a need to do that.

如果您使用故事板,您通常不需要创建单独的 .xib 文件。如果您愿意,您仍然可以使用 .xib 文件——它可能很有用,例如,如果您有一个特别复杂的视图,您想自行配置。不过,在大多数情况下,没有必要这样做。