使用 XIB 和以编程方式执行视图的优缺点

发布于 2024-11-15 14:18:49 字数 206 浏览 3 评论 0原文

我想决定是使用 XIB 更好还是完全使用代码设计我的视图。

到目前为止,我已经读到,当您在界面生成器上设计视图时,它们是预先构建的,因此即使它们使用更多内存,用户也会感觉一切都更快。

人们说使用代码做所有事情都比较困难,但我发现它同样容易,所以我想知道是否有人在使用笔尖时体验到了一些真正的速度提升。

您的经验、建议等是什么?

谢谢!

I want to decide if it is better to use XIBs or to designs my views completely using code.

So far I have read that when you design your views on interface builder they are pre-built, so even if they use more memory the user feels everything is faster.

People say doing everything using code is harder but I find it to be just as easy, so I want to know if anyone has experienced some real speed gains when using nibs.

What have been your experiences, advice, etc?

Thanks!

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

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

发布评论

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

评论(5

旧竹 2024-11-22 14:18:49

您应该能够做到这两点 - 有时以编程方式构建视图更好/更容易,有时使用 .xib 更好/更容易。即使您只以一种方式做事,您也会遇到用另一种方式做事的代码,并且您需要能够处理它。

如果您不知道如何使用 IB,那么在代码中构建视图肯定会更容易。这就是为什么你应该学习使用IB。一旦您了解了 IB,就可以更快地将您的应用程序可能需要的大部分基于视图的 UI 组合在一起。 IB 可以帮助您排列事物、居中对象、对齐基线、将控件连接到其目标和操作等。我认为可以肯定地说,每个使用 IB 的人都能有效地体验到“使用 IB 时的真实速度增益”笔尖。”

You should be able to do both -- there are times when building a view programmatically is better/easier, and times when using a .xib is better/easier. Even if you only ever do things one way, you'll run into code that does it the other, and you'll need to be able to deal with that.

If you don't know how to use IB, then building your views in code is certainly easier. That is why you should learn to use IB. Once you understand IB, it's way, way faster to put together most of the view-based UI your app will likely need. IB helps you line things up, center objects, align base lines, connect controls to their targets and actions, etc. I think it's safe to say that everyone who uses IB effectively experiences "real speed gains when using nibs."

野の 2024-11-22 14:18:49

您应该知道如何使用两者。两者之间的性能差异可以忽略不计,不应成为您选择其中之一的原因。

许多刚接触 iOS 开发的人都有这样的误解:nib(.xib 文件)不如以编程方式创建 UI,并且如果您使用 IB,那么您就不是一个优秀的 iOS 开发人员。这种观点是100%错误的。 IB 由 Apple 创建,Apple 开发人员使用 IB 创建他们自己的 Mac OS X 和 iOS 应用程序。如果 IB(作为一种工具)足以被世界上一些最好的开发人员使用,那么它对于我们大多数人来说可能也足够好了。

在实践中,我发现两者的结合通常符合要求。

在我自己的应用程序中,我发现 .xibs 非常适合快速布置视图的基础知识,它们允许您快速迭代,同时预览视图的外观。在 .xib 文件中使用自动布局也更容易。

然后,当您需要做更高级的事情(例如添加精美的动画或移动视图)时,这就是 IBOutlet 的用途。放入 nib 中的任何内容都可以通过 IBOutlet 进行引用。这允许您以编程方式使您的视图变得生动。

最后,您应该完全理解笔尖 (.xib) 会自动为您做什么。您应该了解 .xib 的对象解冻时会发生什么。互联网上有许多资源可以帮助您更好地理解 .xib 文件。

另外,了解如何以封装的方式使用 .xibs。例如,.xibs 对于原型单元之类的东西非常有用,它们允许您保持代码库模块化(比故事板更模块化)。此外,您的视图控制器中将需要更少的 UI 代码。

最后,我总是说人们应该像 jQuery 一样看待 IB/.xibs。它会节省你很多时间,但最好的开发人员仍然知道如何在 javascript 中完成所有工作(如果需要的话)。

祝你好运,玩得开心!

TL;DR 版本

  • 在决定是否使用 .xibs 时,性能不是考虑因素。
  • 使用 .xibs 是因为它们可以为您提供正在创建的视图的预览,并且允许您快速迭代。
  • 实际上,大多数应用程序都会使用两者的组合。您将以编程方式添加动画或移动视图,但 .xibs 将是一个起点。
  • 充分了解 .xib 中的对象解冻时
  • 会发生什么情况您的工作效率会更高,但请确保您完全了解幕后发生的情况。

You should know how to use both. Performance differences between the two are negligible and should not be the reason that you choose one or the other.

Many people who are new to iOS development have the misconception that nibs (.xib files) are inferior to programmatically creating your UI and that if you use IB you're not a good iOS developer. That view is 100% wrong. IB is created by Apple and in use by Apple's developers to create their own Mac OS X and iOS apps. If IB (as a tool) is good enough to be used by some of the best developers in the world, it's probably good enough for most of us.

In practice I have found that a combination of the two usually fits the bill.

In my own apps I find that .xibs are great for laying out the basics of your views quickly and they allow you to iterate very quickly while giving you a preview of what your view will look like. It's also much easier to use auto layout in a .xib file.

Then when you need to do more advanced things like add fancy animations or move views around that is what IBOutlets are for. Anything that you put into a nib can be referenced through an IBOutlet. This allows you do then programmatically make your view come to life.

Lastly, you should fully understand what a nib (.xib) is doing automagically for you. You should understand what happens when a .xib's objects are unfrozen. There are many resources on the internet to understand .xib files better.

Also, learn how to use .xibs in an encapsulated way. For example, .xibs are crazy useful for things like prototype cells and they allow you to keep your code base modular (much more so than storyboards). Also, you will require less UI code in your view controllers.

Lastly, I always say that people should think of IB/.xibs like jQuery. It's going to save you a lot of time but the best developers still know how to do everything in javascript if they have to.

Good luck and have fun!

TL;DR version

  • Performance is not a consideration when deciding to use .xibs or not.
  • Use .xibs because they give you a preview of the view you are creating and they allow you to quickly iterate
  • In practice most apps will use a combination of both. You will programmatically add animations or move views around but the .xibs will be a starting point
  • Understand fully what happens when the objects in a .xib are unfrozen
  • You'll be more productive but be sure you fully understand what is happening behind the scenes.
夜唯美灬不弃 2024-11-22 14:18:49

我总是会使用 XIB 文件,除非有理由不这样做。这使您的视图将来可以轻松维护。

以编程方式创建视图的一些原因可能是:

  • 需要调整控件的大小,
    重新定位或以其他方式改变
    取决于其他
  • 东西
    需要添加或删除
    动态的

原因可能更多,但也不会太多。

如果在不需要时以编程方式创建视图,那么其他开发人员尝试弄清楚视图的外观并对其进行更改会变得更加困难。

I would always use XIB files unless there was a reason not to. This allows your views to be maintained easily in the future.

Some reasons for creating the views programmatically might be:

  • A control needs to be resized,
    repositioned or otherwise altered
    depending on something else
  • Controls
    need to be added or removed
    dynamically

There may be more reasons but there are not too many.

If you programmatically create views when there is no need you make it a lot more difficult for other developers to try to figure out what the view will look like and to change it.

横笛休吹塞上声 2024-11-22 14:18:49

如果您以编程方式构建视图,则可以控制元素的加载。例如,您可以使用延迟加载,并在更重要的元素之后加载辅助按钮、子视图等,从而使 UI 的关键部分更快地出现。您甚至可以将某些元素设置为动画。

如果您使用 IB,您将获得有关正确元素间距和定位的指南,但如果您不经常更改设计,则始终可以将坐标从 IB 复制到代码中。

对于简单的 UI 元素,如果以编程方式创建它们,最终将需要维护更多行代码。

If you build your views programmatically, you have control over the loading of elements. e.g. you could use lazy loading, and load secondary buttons, subviews, etc. a fraction of a second after the more important elements, allowing the key parts of the UI to come up faster. You could even animate some elements into position.

If you use IB, you get guides as to proper element spacings and positioning, but you could always copy the coordinates from IB into code if you aren't changing the design that often.

For simple UI elements, you will end up with more lines of code to maintain if you create them programatically.

遗失的美好 2024-11-22 14:18:49

IB 和 NIB 在优化视图的加载/卸载方面做了很多工作,但它主要面向最大限度地减少内存使用与用户感知的速度。例如,延迟加载(如果有的话)可能会使应用程序 UI 稍微变慢,但它应该会降低内存使用量。这反过来可以使大型应用程序的整体应用程序性能更好,并且非常值得鼓励,但很难以狭隘的方式定义“性能”。也很难说什么时候应该使用 IB,什么时候不应该使用 IB - 有时在代码中使用 IB 会更好。

对于 IB 是否存在争议,一个经常被忽视的因素是开发速度,尤其是当您有多个开发人员时。在较大的团队/项目中,您可能会有一些更专注于应用程序的基础设施、业务逻辑等的开发人员,以及一些更专注于 UI 的开发人员。在这种情况下,使用IB将使他们更容易独立工作,这应该会使整体开发更加高效。

我将IB视为iOS开发的开发平台的核心部分。这并不是在所有情况下都是正确的解决方案,但不知道如何使用 IB 将是一个真正的限制因素。

IB and NIBs do a lot to optimise loading/unloading of views, but it is largely oriented to minimising memory usage vs. perceived speed for the user. For example, lazy loading if anything might make the app UI slightly slower, but it should make memory usage lower. This in turn could make overall app performance better on a large application, and is very much encouraged, but it's difficult to define "performance" in a narrow way. It's also difficult to say when you should or should not use IB - there will be some times you're much better off doing it in code.

One often overlooked element to the IB or not debate is development speed, especially if you have multiple developers. On a larger team/project you'll probably have some developer(s) who specialise more in the infrastructure, business logic etc. of the app and some developer(s) who specialise more in the UI. In this case, use of IB will make it easier for them to work independently, which should make overall development more efficient.

I view IB as a core part of the development platform for iOS development. It's not the right solution in every situation but not knowing how to use IB will be a real limiting factor.

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