ios 故事板与旧的 XIB 方式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13834999/
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
Storyboards vs. the old XIB way
提问by cdub
I am new to iOS and was wondering which is the best to learn. I have read some of the answers here and SO, but some people say use Storyboards will others say learn XIBs first. Is there any real benefit to learning XIBs? Are XIBs easier to understand and will help with storyboards?
我是 iOS 新手,想知道哪个最好学。我已经阅读了这里和SO的一些答案,但有些人说使用故事板会其他人说先学习XIB。学习XIB有什么真正的好处吗?XIB 是否更容易理解并且对故事板有帮助?
回答by hsoi
There is benefit to learning both approaches.
学习这两种方法都有好处。
Apart from the historical value in the xib approach, xib's also provide modularity. Perhaps you have a library of code or wish to share a useful widget you made. Taking the xib approach would facilitate that sharing and reuse.
除了xib 方法的历史价值外,xib 还提供模块化。也许您有一个代码库或希望分享您制作的有用小部件。采用 xib 方法将促进这种共享和重用。
The xib approach also allows you some greater flexibility in terms of your own code. For example, iOS 5 contained a bug with UITableView
and Accessibility/VoiceOver support that would cause -dequeueReusableCellWithIdentifier:
to return nil
despite being documented otherwise (see this blog postfor further details). To dynamically load table view cells from xib provided the ability to work around the bug.
xib 方法还允许您在自己的代码方面具有更大的灵活性。例如,iOS 5 包含一个带有UITableView
辅助功能/VoiceOver 支持的错误,尽管另有说明,该错误仍会导致-dequeueReusableCellWithIdentifier:
返回nil
(有关更多详细信息,请参阅此博客文章)。从 xib 动态加载表格视图单元格提供了解决该错误的能力。
While the table and tablecell support in Storyboards is wonderful and provides support for what most people need to do in a table, sometimes you have to color outside the lines, you might need lots of different cells, and again, dynamically loading from xibs can be your solution.
虽然 Storyboards 中的 table 和 tablecell 支持很棒并且为大多数人需要在 table 中做的事情提供支持,但有时您必须在线条外着色,您可能需要许多不同的单元格,并且再次从 xibs 动态加载可以是你的解决方案。
One big advantage of Storyboard is the ability to view your entire application's GUI flow. Zoom out and you can see how everything interconnects and flows. With xibs, while the modularity is nice, it's tougher to envision how everything connects and flows together. This can be a useful feature for yourself, or if you have a larger team to share with, to allow others to see how the app flows.
Storyboard 的一大优势是能够查看整个应用程序的 GUI 流程。缩小,你可以看到一切是如何相互连接和流动的。使用 xibs,虽然模块化很好,但更难想象一切如何连接和流动在一起。这对您自己来说可能是一个有用的功能,或者如果您有一个更大的团队要共享,可以让其他人看到应用程序的流程。
There's value in both approaches, and it's good to know both so you can pick the best tool for your task at hand.
这两种方法都有价值,最好同时了解这两种方法,这样您就可以为手头的任务选择最佳工具。
Update 2014-10-06- Since I wrote the above, I've been involved in more projects. Some with xib, some that could use storyboards.
2014年 10 月 6 日更新- 自从我写了上面的内容,我参与了更多的项目。有些带有xib,有些可以使用故事板。
Storyboards have matured a great deal (we're at Xcode 6 now), and there's a great deal with them that's so nice. I really love how so much more can be done within a storyboard that is a bit more complicated in a xib-approach. A couple examples:
故事板已经成熟了很多(我们现在在 Xcode 6 上),并且有很多很棒的东西。我真的很喜欢在一个故事板中可以做更多的事情,而在 xib 方法中它有点复杂。几个例子:
One is when working with UITableView
or UICollectionView
how much you can work with prototype cells directly in the storyboard. A lot of nice and easy setup, most of the heavy lifting can be in the storyboard, less code. It's quite nice. Trying to do this in the xib approach is certainly do-able, but there's a lot more work to make it happen.
一是在故事板中直接使用原型单元格时UITableView
或UICollectionView
您可以使用多少原型单元格。很多漂亮和简单的设置,大部分繁重的工作都可以在故事板中,更少的代码。这很不错。尝试在 xib 方法中做到这一点当然是可行的,但要实现它还有很多工作要做。
Another is how nicely you can transition between UIViewController
s with the regular segues then go back with unwind segues. All right there in the storyboard, with minimal code. It's just so handy.
另一个是您可以在UIViewController
s 与常规 segues之间进行转换,然后用 unwind segues 返回。就在故事板中,代码最少。真是太方便了。
But the one thing that still kills storyboards for me is trying to use them in collaborative environments. It's just not going to merge well. And in some regard, it's not even if you're working on a team of > 1 person. If you yourself take advantage of version control, use a good branching and merging model for your own personal workflow, there may come a time where some change is going to have to be made in some branch that has to be brought into another branch, and oh the pain. To me, this is what kills storyboards.
但是对我来说仍然会杀死故事板的一件事是尝试在协作环境中使用它们。它只是不会很好地合并。在某些方面,即使你在一个超过 1 人的团队中工作也不是这样。如果您自己利用版本控制,为您自己的个人工作流程使用良好的分支和合并模型,那么可能会有一段时间必须在某些分支中进行某些更改,并且必须将其引入另一个分支,并且哦,痛苦。对我来说,这就是杀死故事板的原因。
As time and work has evolved, what I'm finding for myself is storyboards are great for prototyping. The ability to get things going quickly is a huge benefit of storyboards. There's much speed in using them. But the speed comes at cost. When it comes to writing the "real" code for some project, I'm just going to stick with xibs because while it may be more work, it's a more flexible route that just works better in larger teams or over time.
随着时间和工作的发展,我发现故事板非常适合原型制作。让事情快速进行的能力是故事板的一个巨大好处。使用它们的速度很快。但速度是有代价的。当谈到为某个项目编写“真正的”代码时,我将坚持使用 xibs,因为虽然它可能需要更多的工作,但它是一种更灵活的方法,在更大的团队中或随着时间的推移会更好地工作。
Update 2015-04-07Another update, because the projects of the past some months have forced me to use storyboards, which has provided more insights.
更新 2015-04-07另一个更新,因为过去几个月的项目迫使我使用故事板,这提供了更多的见解。
First, some things will mandate one approach or another. For example, apparently there were some edge case bugs when working with size classes in xibs that didn't exist doing the same thing in a storyboard. So if you get affected by bugs, that may force your hand one way or the other. Another is to remember that storyboards generally work on the UIViewController
level, so if you need to do something like have a UIView
or a UICollectionViewCell
to load, that's probably going to be better served by a xib.
首先,有些事情会要求采用一种或另一种方法。例如,显然在使用 xibs 中的大小类时存在一些边缘案例错误,而在故事板中不存在做同样的事情。因此,如果您受到错误的影响,这可能会迫使您的手以一种或另一种方式进行。另一个是记住故事板通常在UIViewController
关卡上工作,因此如果您需要执行诸如加载aUIView
或 a 之类的UICollectionViewCell
操作,xib 可能会更好地提供服务。
Second, and I don't know why this didn't occur to me at first, but there is nothingthat requires you to use a singular storyboard for your entire project! I think the nature of storyboard enables people to gravitate that way, but we have to remember nothing mandates that (that I'm aware of).
其次,我不知道为什么一开始我没有想到这一点,但是没有什么需要您为整个项目使用单一的故事板!我认为故事板的性质使人们能够以这种方式被吸引,但我们必须记住没有任何要求(我知道)。
What I've found works well is to generally approach each "view grouping" per storyboard. That is, often your ViewControllers tend to be isolated and wind up being 1 per storyboard (or xib). But you might have a situation where you have two closely-related ViewControllers, and it makes sense to put them into the same storyboard, especially because then you can easily hook things up between them, such as segues.
我发现效果很好的是通常处理每个故事板的每个“视图分组”。也就是说,通常您的 ViewControllers 往往是孤立的,并且每个故事板(或 xib)最终是 1 个。但是你可能会遇到这样的情况,你有两个密切相关的 ViewController,把它们放在同一个故事板中是有意义的,特别是因为这样你可以很容易地在它们之间连接东西,比如 segue。
The main advantage to multiple storyboards? Working in teams. This way Fred can work on his storyboard and Wilma can work on her storyboard, and there's no strong worries of merge problems or work coordinatinon! The use of multiple storyboards (and generally 1 ViewController per storyboard) has been a huge help in the use of storyboards on a multi-person dev team.
多个故事板的主要优势是什么?在团队中工作。这样 Fred 可以处理他的故事板,Wilma 可以处理她的故事板,并且没有合并问题或工作协调的强烈担忧!使用多个情节提要(通常每个情节提要 1 个 ViewController)对多人开发团队中情节提要的使用有很大帮助。
It's pretty evident Apple wants us to prefer storyboards, and I'm embracing them more these days. Using multiple storyboards, but still using a xib when needed, is working fairly well now.
很明显,苹果希望我们更喜欢故事板,而我最近越来越喜欢它们。使用多个故事板,但在需要时仍使用 xib,现在工作得相当好。
Update 2015-09-21Now that Apple's released Xcode 7, there's even more reason to adopt storyboards, as Apple works to overcome the shortcomings.
更新 2015-09-21既然 Apple 发布了Xcode 7,就有更多理由采用故事板,因为 Apple 正在努力克服这些缺点。
The most important improvement is storyboard references, which allow you to create in one storyboard a reference to another storyboard. It's dead simple to make, and now you can have cross-storyboard segues (both entrance and exit). I've used this a few times already on a new project and it's just a joy.
最重要的改进是情节提要引用,它允许您在一个情节提要中创建对另一个情节提要的引用。制作起来非常简单,现在您可以拥有跨故事板的转场(入口和出口)。我已经在一个新项目中使用了几次,这只是一种乐趣。
Another improvement is that you can create stand-alone UIView
classes within a storyboard. However, as of this writing I've had mixed results with it. Simple cases work out ok, but some more "complicated" stuff did not. For example, I had a UIViewController
with a UITableView
within it. Since it was to be a simple table with 5 static cells, I merely instantiated the 5 UITableViewCell
s as a part of the ViewController in the storyboard. Seemed to work, but then at runtime nothing would actually load and show up; moved the UITableViewCell
s into a xib, and all worked. I'm not sure if I was doing something wrong or what it may be, so YMMV. But still, even if there's just some quirks, in time I'm sure Apple will resolve them and then another barrier against storyboards will fall. I would say that if you need such support, you should try it and see how it goes for you. There's great promise.
另一个改进是您可以UIView
在故事板中创建独立的类。然而,在撰写本文时,我的结果喜忧参半。简单的案例可以解决问题,但一些更“复杂”的东西却没有。例如,我有一个里面UIViewController
有一个UITableView
。由于它是一个包含 5 个静态单元格的简单表格,我只是将 5 UITableViewCell
s实例化为故事板中 ViewController 的一部分。似乎有效,但在运行时实际上什么也不会加载和显示;移动了UITableViewCell
s 变成了一个 xib,并且一切正常。我不确定我是否做错了什么或可能是什么,所以 YMMV。但是,即使只是一些怪癖,我相信苹果会及时解决这些问题,然后另一个阻碍故事板的障碍就会消失。我会说,如果你需要这样的支持,你应该尝试一下,看看它对你来说如何。有很大的承诺。
More and more, storyboards are shaping up to be excellent.
越来越多的故事板正在变得非常出色。
回答by rob mayoff
There are things you can do with a storyboard that you can't do with a nib. A storyboard lets you create segues between view controllers, and it lets you design table view cells in-place.
有些事情你可以用故事板做而你不能用笔尖做。故事板允许你在视图控制器之间创建转场,它允许你就地设计表格视图单元格。
There are things you can do with a nib that you can't do with a storyboard. In a nib, you can create references to the File's Owner placeholder. You can create multiple top-level views, edit them, and create connections between them. See this answerfor an example of why you'd want to do that. You can add external object placeholders (a rarely-used feature).
有些事情你可以用笔尖做,而故事板却做不到。在笔尖中,您可以创建对文件所有者占位符的引用。您可以创建多个顶级视图、编辑它们并在它们之间创建连接。 有关为什么要这样做的示例,请参阅此答案。您可以添加外部对象占位符(很少使用的功能)。
Storyboards have the drawback that they collect a bunch of different, loosely-related objects into one big file. If you're working on a project with several developers, you are much more likely to run into merge conflicts if you're using a storyboard than if you're using xib files.
故事板的缺点是它们将一堆不同的、松散相关的对象收集到一个大文件中。如果您正在与多个开发人员一起处理一个项目,那么与使用 xib 文件相比,如果您使用故事板,则更有可能遇到合并冲突。
You should definitely learn about nibs at some point. Whether you want to start with them or start with a storyboard is probably not too important. Just find some tutorials you like and work through them with whichever type of file (nib or storyboard) they use.
你绝对应该在某个时候了解笔尖。无论您是想从它们开始还是从故事板开始可能都不太重要。只需找到一些您喜欢的教程,并使用他们使用的任何类型的文件(笔尖或故事板)来完成它们。