应该或不应该使用 nib 文件?

发布于 2024-12-26 19:42:08 字数 144 浏览 5 评论 0原文

我是 iOS 开发新手。我已经阅读了一些在网上找到的源代码,其中很多不包含单个 nib 文件。所有视图似乎都是从代码手动绘制的。

有/没有 nib 文件有什么好处?为什么他们选择从代码创建所有内容,而不是可以可视化的内容(例如情节提要或 *.xib 文件)?

I'm new to iOS development. I've read through some of the source code I've found online and lots of them does not include a single nib file. All the view seems to be drawn manually from code.

What is the benefits of having/not having a nib file? Why did they choose to create everything from code instead of something you could visualize such as storyboard or *.xib files?

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

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

发布评论

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

评论(6

德意的啸 2025-01-02 19:42:08

我喜欢代码的最大原因是它在源代码管理中差异很大。 XIB 很难区分、合并、读取。

代码也很容易复制/粘贴保存的代码片段。对于 IB,我总是忘记一些复选框,这让我想知道为什么魔法不起作用。我的笔记有代码片段。

IB 真正擅长的是布局并帮助您制定人机界面指南(控件之间的像素数等......带有指南)。

所以,我个人的偏好是 IB 中的布局和代码中的其他所有内容(包括目标、操作、添加列等......等等......)。但在动态场景下,IB 会崩溃,您最终会得到自定义视图。

这是相关帖子:

Interface Builder (XIB)或者在团队环境中合并时的代码?

The biggest reason I like code is it diffs well in source control. XIBs are very hard to diff, merge, read.

Code is also easy that copy/paste saved snippets. With IB, I always forget some check box which leaves me wondering why the magic isn't working. My notes have code snippets.

What IB really excels at is layout and helping you with human interface guidelines (number of pixels between controls etc... with guiedlines).

So, my personal preference is layout in IB and everything else in code (including target, action, adding columns etc... etc...). Under dynamic scenarios though, IB falls apart and you end up with custom views.

Here's a related post:

Interface Builder (XIB) or Code when merging in a team environment?

不必了 2025-01-02 19:42:08

关于为什么应该使用笔尖和不应该使用笔尖的原因有很多。没有明确的答案,每个答案都取决于您需要做什么。

除了 Nibs 提供的明显优势(快速的 UI 创建过程、最小化 .m 文件中的视图构建代码)之外,它们还提供了您无法通过其他方式找到的东西:解决本地化问题。在将应用程序本地化为其他语言时,您会偶然发现需要 2-3 个单词才能解释的短语和事物,而在另一种语言中则只需一个单词即可解释。当使用不同的本地化时,这会严重导致视图控制器内视图错位的错误。因此,您可以在 Xcode 4 中为每个笔尖设置 2-3 个子笔尖,并按照您喜欢的方式本地化每个笔尖,并将按钮和视图放置在正确的位置,而不必担心根据语言来重新定位视图。用户有。如果您要使用代码来完成所有这些操作,那么您应该到处都放置“if”,这肯定是一种不好的编程习惯,并且容易出错。

我创建了一些视图控制器,如果我不使用界面生成器,则需要数百行来设置视图。

然而,NIB 永远无法达到以编程方式创建视图的性能,因为每个 NIB 都是用 HTML/XML 编写的视图描述符,并且在创建任何视图之前,必须从磁盘读取文件并进行分析。 Nib 还缺乏纯代码所具有的自定义选项(阴影、圆角和其他 Quartz 魔法)。这些自定义选项不可用,因为有很多方法可以使用代码实现相同的结果,要么通过与更高级别的核心动画层对话,要么通过直接寻址 QuartCore 和 CGGraphics 并在那里做繁重的事情,这肯定更快,并且在大多数情况下推荐情况(使用图层的阴影可能非常慢)。因此,苹果不想将开发限制在特定的绘制方式上。

NIB 的存在是有原因的。您必须确保在您的申请中了解创建 NIB 的原因。 Nib 的存在是为了将代码连接到插座、促进本地化、加速开发和清理代码。在项目中,您当然必须使用 Nib,但您也必须避免使用它们,因为纯代码也可以用最少或类似的工作为您提供相同的结果。

最后但并非最不重要的一点是,考虑内存管理。使用 Nib 会影响 IBOutlet 等已分配对象的释放。如果您确定您创建的 IBOutlet 将在您需要时被释放,请不要使用 NIB。请改用纯代码。

There are many reasons on why you should use nibs and why you shouldn't. There is no definitive answer, and each answer depends on what you need to do.

Apart from the obvious advantages Nibs offer (speedy UI creation process, minimize view construction code in .m files) they offer something you can't find any other way: Localization problems solving. While localizing your application to other languages, you will stumble across phrases and things that take 2-3 words to explain while in another language they take just one. That seriously leads to errors with misplaced views inside a view controller when using different localizations. So, you can have 2-3 sub-nibs for each nib in Xcode 4, and localize every one the way you like it, and place the buttons and views in the correct spots, without worrying about relocating your views depending on the language the user has. If you were to do all this using code, you should have placed 'if's everywhere, and that is certainly a bad programming practice, and error prone.

I have created some view controllers that would require hundreds of lines just to set up the views if I didn't use interface builder.

However, NIBs will never achieve the performance of creating the views programmatically since every NIB is a view descriptor written in HTML/XML and before any view is created, a file must be read from the disk and analyzed. Nibs also lack the customization options that the plain code has (drop shadows, round corners, and other Quartz magic). These customization options are not available because there are many ways to achieve the same result using code, either by talking to the higher level Core Animation layer, either by addressing QuartCore and CGGraphics directly and doing heavy stuff there which is certainly faster and recommended in most cases (shadows using layers can be extremely slow). So Apple does not want to constraint development into a specific way of drawing things.

NIBs exist for a reason. You must make sure that in your application you understand the reasons you create a NIB. Nibs exist to connect code to outlets, facilitate localization, speed up development and clean up your code. Inside a project you must certainly use Nibs, but you must also avoid using them where plain code would also give you the same results with minimum or similar effort.

Last but not least, take memory management into consideration. Using Nibs will affect the deallocation of allocated objects like IBOutlets. If you arena's sure that an IBOutlet you create will be deallocated when you want to, do not use a NIB. use a plain code instead.

骄兵必败 2025-01-02 19:42:08

在代码中创建视图的原因有很多。

  • nib 文件是延迟加载的,有时会导致明显缺乏反应,用户不喜欢
  • 您无法在 IB 中配置所有内容,而且很多视图需要一些花哨的额外功能
  • ,有时只写下您的视图比单击并拖动所有需要的内容更容易东西放在一起
  • ...

我认为最重要的原因是性能和功能的缺乏。

当我只想通过简单的按钮和标签来显示简单的信息以及进行原型设计时,我会使用 nib 文件。

there are many reasons to create views in code.

  • nib-files are lazy-loaded and lead sometime to a noticeable lack of reaction which users dont like
  • you cannot configure everything in IB and a lot Views need some fancy extras
  • sometimes it is easier to just write your view down than clicking and dragging all needed stuff together
  • ...

I think the most important reason is the lack of performance and features.

I am using nib-files when I just want to show easy information with just simple buttons and labels and for prototyping.

时光匆匆的小流年 2025-01-02 19:42:08

有一些非常简单的笔尖,例如典型的 MainWindow.nib,它们没有本地化内容,并且可以使用一行代码进行复制。在这种情况下,代码比解压笔尖要快得多。

还有一些高度动态的布局,无法用笔尖来描述。

您应该根据具体情况选择最方便、最有效的方式。总会有一些权衡,只是明智地选择。

There are very simple nibs like the typical MainWindow.nib, which don't have localized content and can be reproduced with a single line of code. In such cases code is much faster than unarchiving the nib.

There are also highly dynamic layouts which are impossible to describe as a nib.

You should choose the most convenient and efficient way in each particular case. There will always be tradeoffs, just choose wisely.

白首有我共你 2025-01-02 19:42:08

nib 文件是一种特殊类型的资源文件,用于 iOS 和 Mac 应用程序的用户界面。 nib 文件是 Interface Builder 文档。您可以使用 Interface Builder 来设计应用程序的可视部分(例如窗口和视图),有时还可以配置非可视对象,例如应用程序用于管理其窗口和视图的控制器对象。实际上,当您编辑 Interface Builder 文档时,您会创建一个对象图,然后在保存文件时将其存档。当您加载文件时,对象图将被取消归档。

nib 文件(以及对象图)可能包含占位符对象,这些占位符对象用于引用存在于文档外部但可能引用文档中的对象,或者引用文档中的对象。一个特殊的占位符是文件的所有者。

在运行时,您可以使用方法 loadNibNamed:owner: 或其变体加载 nib 文件。文件所有者是 nib 文件中作为该方法的所有者参数传递的对象的占位符。无论您在 Interface Builder 的 nib 文件中与文件所有者建立了何种连接,当您在运行时加载该文件时,都会重新建立该连接。

iOS 使用 nib 作为支持 Storyboard(iOS 用户界面设计布局格式)的实现细节。故事板允许您在一张画布上设计和可视化应用程序的整个用户界面。对于 iOS 开发人员来说,使用 Storyboard 是设计用户界面的推荐方法。

A nib file is a special type of resource file that you use to the user interfaces of iOS and Mac apps. A nib file is an Interface Builder document. You use Interface Builder to design the visual parts of your app—such as windows and views—and sometimes to configure non-visual objects, such as the controller objects that your app uses to manage its windows and views. In effect, as you edit an Interface Builder document, you create an object graph that is then archived when you save the file. When you load the file, the object graph is unarchived.

The nib file—and hence the object graph—may contain placeholder objects that are used to refer to objects that live outside of the document but that may have references to objects in the document, or to which objects in the document may have references. A special placeholder is the File’s Owner.

At runtime, you load a nib file using the method loadNibNamed:owner: or a variant thereof. The File’s Owner is a placeholder in the nib file for the object that you pass as the owner parameter of that method. Whatever connections you establish to and from the File’s Owner in the nib file in Interface Builder are reestablished when you load the file at runtime.

iOS uses nibs as an implementation detail that supports storyboards, the iOS user interface design layout format. Storyboards allow you to design and visualize the entire user interface of your app on one canvas. For iOS developers, using storyboards is the recommended way to design user interfaces.

嘿嘿嘿 2025-01-02 19:42:08

我将从 xibs 开始。完成 UI 后,即可将 xib 迁移到代码。这样您就可以两全其美。 XIB 可能很慢,我见过 400KB XIBS(虽然有点罕见)。 XIB 确实很糟糕……

I would start with xibs. Once you have finalized the UI, then migrate your xibs to code. This way you get the best of both worlds. XIBs can be slow, and I've seen 400KB XIBS (kinda rare though). XIBs are definitely crufty though...

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