在 iOS 编程中使用 Storyboard 代替 xib 文件有什么好处?
使用 Storyboard 和 xib 文件之间的主要区别是什么?
具体来说, 使用故事板的优点或缺点是什么?
不幸的是,尽管做了相当多的研究,我在故事板上找到的只是简单的教程,向您展示如何设置故事板,而不是解释它们是什么的具体信息。
What are the main differences between using Storyboards and xib files.
Specifically,
what are the advantages or disadvantages of using a Storyboard?
Unfortunately, despite doing quite a bit of research, all I've been able to find on Storyboards are simple tutorials that show you how to set up a Storyboard, instead of concrete information explaining what they are.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
故事板是:
我已经使用 Storyboards 一段时间了,唯一的缺点是你无法针对 iOS 4 或更低版本。故事板仅适用于运行 iOS 5 或更高版本的设备。除此之外,在我看来,好处很多,缺点也不存在。
我见过的最好的教程是 Ray Wenderlich 的
另外,如果您是 Apple Developer 计划的成员,请查看去年的 Storyboards (iTunesU) WWDC 会议,非常棒。
另一个很棒的课程(也在 iTunesU 上)是最新的斯坦福 iOS 应用程序编程课程。
A Storyboard is:
I have been using Storyboards for awhile now and the ONLY downside is that you can't target iOS 4 or below. Storyboards only work on devices running iOS 5 or better. Other than that, the benefits are many and the downsides are non-existent IMO.
The best tutorial I have seen is Ray Wenderlich's
Also, if you are a member of the Apple Developer program, check out last years WWDC session on Storyboards (iTunesU), it is awesome.
Another great one (also on iTunesU) is the latest Stanford iOS Application Programming course.
故事板不仅有优点,也有缺点——只是因为你要求输入:
-以下说法不正确:
-如果您需要做 SB 不提供的事情,那么将 SB 与编程创建的视图混合起来并不容易(不过,这是可能的)
经验法则似乎是:越多你期望你的项目越复杂,你最好不要选择SB。
编辑:
- SB 的另一个缺点:解决 XCode 关于 SB 的所有烦人的错误。例如,由于存在一些不一致之处,必须经常刷新 DerivedData 文件夹。有时故事板文件或它们的链接会损坏。然后你可能会很高兴去寻找问题。看看这个线程来获取这个想法
编辑 2(2013 年 3 月):同时 Storyboards 和 Xcode 工作得更好,文档和最佳实践也得到了广泛传播。我认为大多数项目都可以推荐使用故事板,即使仍然存在一些问题。
编辑 3(2013 年 9 月):现在使用新的 Xcode 5 格式与 SB 合作可能会变得更好,因为它似乎可以 合并 SB 代码更容易现在。
另一个编辑:好吧,如果你有一个小时的时间,坐下来,放松一下听听这些人讨论这个话题 (Ray Wenderlich & Co)
编辑 2016.1: 之后长期以来,我一直是 Storyboard 的倡导者,在过去的几个月里,我遇到了很多麻烦,所以我决定尽可能放弃 Storyboard。原因是苹果添加了愚蠢的功能,但并不关心错误和缺陷。具有大量自动布局约束的性能非常糟糕(在设计时),并且容易出错。
示例:即使不太复杂的 Storyboard 在 Xcode 中打开项目后也往往会进入“脏模式”(请参阅 git state)。
提示:作为初学者,您会喜欢故事板,因为您可以快速构建原型并让事情运行而无需大量代码。当您进入中间状态时,您将向项目添加更多 GUI 代码。现在你开始在代码和 SB 之间来回切换——事情开始变得更糟。迟早您会倾向于在代码中完成大部分 GUI 工作,因为结果比拥有多个源更可预测。
There are not only pro sides of Storyboarding, also cons - just because you asked for input:
-The following is not true:
- if you need to do things SB doesn't offer, it's not quite easy to get SB mixed with programatical created views (well, it is possible though)
The rule of thumb seems to be: the more complex you expect your project to get, the more you'll better not go for SB.
EDIT:
- another disadvantage of SB: working around all the annoying bugs of XCode regarding SB. E.g. having to frequently flush the DerivedData folder because of several inconsistencies. Sometimes storyboard files or the link to them get corrupted. Then you might have the joy to search for the problem. Take a look at this thread to get the idea
EDIT 2 (March 2013): meanwhile Storyboards and Xcode are working much better, and documentation and best practices are wide spread. I think working with storyboard can be recommended for a majority of projects, even if there are still some glitches.
EDIT 3 (Sept 2013): now with the new Xcode 5 format working in teams with SB might get even better, as it seems to become possible to merge SB-code much easier now.
Another EDIT: well, if you have an hour of time, sit back, relax and listen to these guys discussing this topic (Ray Wenderlich & Co)
Edit 2016.1: after a long time being a Storyboard advocate, I had so much hassle with it the last months, that I decided to abandon Storyboards as far as possible. The reason for that is that Apple adds feature like stupid, but doesn't care about the bugs and the flaws. The performance having a lots of auto layout constraints is really bad (while design time), and the error-prone-ness has become huge.
Example: even less complex Storyboards tend to get into a 'dirty mode' right after opening a project in Xcode (see git state).
Tip: as a beginner you will love Storyboards since you can prototype quickly and get things running without lots of code. As you enter an intermediate state, you'll add more GUI code to your project. Now you start going back and forth between code and SB - and things start working out worse. Sooner or later you'll tend to do most of the GUI stuff in code, because the result is more predictable than having several sources.
摘要
Nibs/.xib 文件和 Storyboard 都是 Interface Builder 文件,用于在 Xcode 中直观地创建 iOS 和 Mac 应用程序的用户界面(我将使用 iOS 术语来表示类,因为这个问题被标记为 iOS,但它也适用于 Mac 编程)。
差异
Nib 旨在与单个 UIView 一起使用。它们还可以通过将文件所有者的类设置为 UIViewController 的任何子类来连接到 UIViewController 子类,并连接视图出口(使用“连接检查器”中的“连接检查器”进行拖动以进行连接) Xcode 的最右侧窗格)。
故事板旨在包含 1 个或多个 UIViewController 的用户界面。您可以在单个故事板中构建整个用户界面或将其分成更小的部分。
优点
故事板应始终用于支持 .xib 文件/Nib(用于视图控制器)。 Storyboards 具有更多功能,并且由 Apple 积极开发。
每个支持 Nib 的论点都依赖于这样一个事实:它们是单独使用的,而故事板包含许多场景。您可以为每个
UIViewController
使用单个故事板,就像使用 Nibs 一样轻松(请参阅下面的代码示例)。继续阅读详细的解释和代码示例。详细
为什么 Storboard 优于 Nib?
答案基本上可以归结为 Apple 鼓励使用 Storyboard 并投入更多的开发精力。
反对故事板的基本论点是,将所有视图控制器放在一个地方会导致合并冲突、Xcode 缓慢、构建时间缓慢,并且维护起来很麻烦。因此,一般建议是为每个 UIViewController 使用 Nib。
但是...您可以为每个 UIViewController 创建故事板。一种常见的做法(至少对我来说)是将所有 UIViewController 初始化隐藏在类方法中(因为其他类不需要知道控制器 Nib/Storyboard 所在文件的名称)。
让我们比较一下可用于创建此类方法的相关代码片段。一行代码就是两者之间的全部区别。
Objective-C
故事板
Nib
用法
Swift
故事板
Nib
用法
论点
我将讨论 Nibs 的所有常见论点;正如我之前提到的,大多数人都赞成单个文件,而不是作为 Nibs 优于 Storyboard 的论点
论点:拥有一个包含大量视图控制器的 Storyboard 将
如果您在一个有多个团队的团队中工作,则会导致合并冲突
人们进行更改
响应:单个故事板不会比单个 Nib 引起更多合并冲突
争论:非常复杂的应用程序在故事板中有很多场景,这会导致巨大的故事板需要永远加载和加载由于它的大小,很难理解。
回应:这是一个很好的观点,但是您可以轻松地将故事板分成更小的部分。 故事板参考看起来很棒可用于将 Storyboard 链接在一起的功能,但它们仅在 Xcode 7/iOS 9+ 中可用。而且,这仍然不是选择单个 Nib 而不是 Storyboard 的理由。
论据:为每个
UIViewController
子类创建一个Nib可以让您重用代码,这样您就不必为故事板中的每个场景设置所有约束和出口。回应:同样,这并不是选择单个 Nib 而不是单个 Storyboard 的理由。
Summary
Nibs/.xib files and Storyboards both are Interface Builder files which are used to visually create user interface for iOS and Mac applications in Xcode (i'll use iOS terminology for classes as this question is tagged iOS but it also applies to Mac programming).
Differences
Nibs are intended to be used with a single
UIView
. They can also be connected to aUIViewController
subclass by settings the class of File's Owner to any subclass ofUIViewController
and connection the view outlet (drag to connect using the Connections Inspector in the far right pane of Xcode).Storyboards are intended to contain the user interface for 1 or more
UIViewController
. You can build your entire user interface in a single storyboard or separate it into smaller parts.Advantages
Storyboards should always be used in favor of .xib files/Nibs (for view controllers). Storyboards have more features and are actively developed by Apple.
Every argument in favor of Nibs rely of the fact that they used individually while storyboards contain many scenes. You can use a single storyboard for each
UIViewController
just as easily as you can with Nibs (see code samples below). Keep reading for a detailed explanation and code examples.Detailed
Why are Storboards superior to Nibs?
The answer basically comes down to Apple encouraging the use of Storyboards and putting more development effort into them.
UITableView
(more info)The basic argument against storyboards is that having all your view controllers in one place leads to merge conflicts, a slow Xcode, slow build times and being a general pain in the butt to maintain. Hence, general advice is to use a Nib for each
UIViewController
.But... You can just create storyboard for each
UIViewController
. A common practice (for me at least) is to hide all the UIViewController initialization in a class method (as no other class needs to know the name of the file where the controller's Nib/Storyboard is located).Lets compare the related code snippets that one might use to create such a method. A single line of code is the entire difference between the two.
Objective-C
Storyboard
Nib
Usage
Swift
Storyboard
Nib
Usage
Arguments
I'll address all the usual arguments for Nibs; as I mentioned earlier, there are mostly in favor of single files, not as an argument for Nibs over Storyboards
Argument: Having a storyboard with lots of view controllers will
cause merge conflicts if you are working on a team with multiple
people making changes
Response: A single storyboard causes no more merge conflicts than a single Nib
Argument: Very complex apps have a lot of scenes in the Storyboard which leads to a giant storyboard that takes forever to load and is barely comprehensible because of it's size.
Response: This is a great point, but you can easily break Storyboards into smaller parts. Storyboard References look like a great feature that can be used to link Storyboards together but they are only available in Xcode 7/iOS 9+. Also, still not a reason to choose individual Nibs over Storyboards.
Argument: Creating a Nib for each
UIViewController
subclass lets you reuse code so you don't have to setup all your constraints and outlets for each scene in your storyboard.Response: Again, not a reason to choose individual Nibs over individual Storyboards.
几个月前的 LiDG 会议上,有一个关于 Storyboard 的精彩演示。
就我个人而言,我认为这是使用新应用程序的方式。存在一些差距,特别是对于非常复杂的应用程序,但优点大多大于缺点。
There was a nice presentation about Storyboard given at the LiDG meeting a couple of months ago.
Personally, I'd say it's the way to go with a new app. There are some gaps, especially for very complex apps, but the pro's mostly outweigh the cons.
故事板的更多好处:
“动态”和“原型”单元。
instantiateViewControllerWithIdentifer:]
缺点是:
当故事板包含大量视图控制器时,在 XCode 中渲染速度很慢
无法为一个视图控制器启用自动布局在故事板中。
Some more benefits of storyboards:
"Dynamic" and "Prototype" cells.
instantiateViewControllerWithIdentifer:]
Downsides are:
Storyboards are slow to render in XCode when they contain lots of view controllers
Autolayout can not be enabled for one view controller in the storyboard.
请注意,如果您使用 Storyboard,您的应用程序将无法向后兼容旧操作系统安装。
Be careful, if you use Storyboards your app is not backwards compatible with older OS installations.
故事板基本上是一种让开发人员的工作变得更轻松的工具。它被编译成一系列 nib 文件,因此性能几乎相当,但作为开发人员能够快速概览整个应用程序流程是非常棒的。
我开始过渡到在新项目中使用故事板,前提是我可以说服客户接受 iOS 5 作为最低版本。这纯粹是因为我更喜欢这样做,并且完成相同任务所需的时间更少。
A storyboard is basically a device to make your job as a developer easier. It is complied into a series of nib files, so the performance is pretty much equivalent, but it's great as a developer to be able to look at a quick overview of your entire application flow.
I'm starting to transition to using storyboards on new projects, providing I can convince the client to accept iOS 5 as a minimum version. This is purely because I prefer to do it this way, and it takes me less time to accomplish the same tasks.
您对自动布局的态度也可能会影响您是否要使用情节提要。使用 xib,您可以在每个 .xib 的基础上启用或禁用自动布局,从而允许在您的应用程序中进行混合,而情节提要将您的选择应用于它们包含的所有视图。
Your attitude toward Auto Layout may also affect whether you want to use Storyboards. Using xibs you can enabled or disable Auto Layout is on a per .xib basis, allowing for a mix within your application, while Storyboards apply your choice to ALL views they contain.
您在一秒钟内即可看到大图。如果有很多 NIB 文件,您可能看不到全局。
更容易维护您的程序。更容易理解其他程序......等等。
You see the big picture in one second. Having many NIB files, well, you don't see the big picture.
Easier to maintain your programs. Easier to understand others programs... among others.
优点:
1) 界面设计非常好
2) 您可以使用 StoryBoard Segues 以一种很酷的方式识别导航/模态关系。
3) 如果您的应用程序支持多个设备,那么这是组织不同视图的好方法。
4) 原型设计是另一个额外的优势。
5)原型UITableViewCell可以节省时间,也减少代码量。
6) 您可以使用StoryBoard在一处看到应用程序的所有屏幕。
7) 您可以轻松查看它们之间的关系
8) 如果您正在处理某人的代码,您可以更好地了解应用程序的流程。
9) 您可以通过应用情节提要中的视网膜形状系数来设置 iPhone 4 和 iPhone 5 的用户界面,而无需一次又一次运行该应用程序。
10) 客户可以在开始开发应用程序之前看到应用程序的原型,这里故事板对你有很大帮助。
缺点:
1)它仅在 iOS 5+ 中可用
2)StoryBoardSegue 有点僵化,您可能会多次使用prepareForSegue。
4)和IB一样,对其他显示引擎和工具包不太友好。
4) 使得共享单个视图或一组视图的设计变得困难——您必须发送全部或不发送任何内容。
5) 对于故事板,您将需要一个大屏幕,特别是在 iPad 的情况下。
6) 将视图从其他应用程序复制到故事板时遇到困难。
7) 当多个开发人员使用
从某些资源复制的git 存储库处理同一个项目时,故事板中出现问题
Advantages:
1) It's very nice to design interfaces
2) You can use StoryBoard Segues to identify navigation/modal relationships in a cool manner.
3) If your app supports multiple devices, it's a good way to organize different views.
4) Prototyping is another added advantage.
5) Prototype UITableViewCell can save time and it reduce the amount of the code too.
6) you can see all the screens of the app at one place by using StoryBoard.
7) You can easily view the relationship among them
8) if you are working on someone's code you can get the better understanding of the flow of the app.
9) You can setup the user interface for iPhone 4 and iPhone 5 by applying the retina form factor from storyboard, without running the app again and again.
10) Clients can see the prototype of the app before start developing it, here storyboard helps you a lot.
Disadvantages:
1) It is only available in iOS 5+
2) StoryBoardSegues are kind of rigid and you may make use of prepareForSegue many times.
4) Like IB, not very friendly with other display engines and toolkits.
4) Makes it hard to share designs for a single view or set of views - you have to send all or nothing.
5) For storyboard you will need a big screen specially in case of iPad.
6) Difficulty while copying views from other apps to storyboard.
7) Problems in storyboard when multiple developers work on the same project by using git repository
copied from some resource
故事板的问题多于好处。以下是他们的问题列表,复制自 iraycd:
故事板失败在运行时,而不是在编译时:您的segue名称有拼写错误或者在故事板中连接错误?它会在运行时爆炸。您使用的自定义 UIViewController 子类在故事板中不再存在?它会在运行时爆炸。如果您在代码中执行此类操作,您将在编译时尽早捕获它们。 更新:我的新工具StoryboardLint 主要解决了这个问题。
故事板很快就会变得混乱:随着项目的发展,故事板变得越来越难以导航。另外,如果多个视图控制器有多个连接到多个其他视图控制器,那么您的故事板很快就会开始看起来像一碗意大利面条,您会发现自己放大和缩小并滚动整个地方以找到您正在寻找的视图控制器并找出哪些 segue 点在哪里。 更新:此问题主要可以通过将 Storyboard 拆分为多个 Storyboard 来解决,如 Pilky 的这篇文章 和 罗伯特·布朗的这篇文章。
故事板使团队工作变得更加困难:因为您的项目通常只有一个巨大的故事板文件,所以让多个开发人员定期更改该文件可能会很令人头痛:更改需要合并并解决冲突。当发生冲突时,很难说如何解决它:Xcode 生成故事板 XML 文件,但它的设计目的并不是人类必须阅读它,更不用说编辑它了。
故事板使代码审查变得困难或几乎不可能:同行代码审查对于您的团队来说是一件很棒的事情。但是,当您对故事板进行更改时,几乎不可能与其他开发人员一起审查这些更改。您所能获取的只是一个巨大 XML 文件的差异。破译真正改变的内容以及这些更改是否正确或是否破坏了某些内容确实很困难。
故事板阻碍代码重用:在我的 iOS 项目中,我通常创建一个类,其中包含我在整个应用程序中使用的所有颜色、字体、边距和插图,以使其具有一致的外观和感觉:如果我必须为整个应用程序调整任何这些值,那么这只是一行更改。如果您在情节提要中设置此类值,则会重复它们,并且当您想要更改它们时需要查找每一个出现的值。您很可能会错过其中一个,因为故事板中没有搜索和替换功能。
故事板让您将所有事情都做两次:您是否正在构建一款可在 iPad 和 iPhone 上运行的通用应用程序?当您使用故事板时,通常会有一个 iPad 版本的故事板和一个 iPhone 版本的故事板。保持两者同步需要您在两个地方进行每个 UI 或应用程序工作流程更改。耶。 更新:在 iOS 8 和 Xcode 6 中,您可以使用适用于 iPhone 和 iPad 的单个 Storyboard。
故事板需要不断的上下文切换:我发现自己在代码中的工作和导航速度比在故事板中快得多。当您的应用程序使用故事板时,您会不断切换上下文:“哦,我想点击这个表视图单元格来加载不同的视图控制器。我现在必须打开故事板,找到正确的视图控制器,创建一个新的segue到另一个视图控制器(我也必须找到),给segue一个名称,记住该名称(我不能在故事板中使用常量或变量),切换回代码并希望我不会输错名称为我的继续prepareForSegue 方法。我多么希望能够在我所在的地方输入这 3 行代码!”不,这不好玩。在代码和故事板(以及键盘和鼠标之间)之间切换会很快变得过时,并且会减慢您的速度。
故事板很难重构:重构代码时,您必须确保它仍然符合故事板的期望。当您在故事板中移动内容时,您只能在运行时发现它是否仍然适用于您的代码。对我来说,好像我必须保持两个世界同步。以我的拙见,它感觉很脆弱,并且阻碍了改变。
故事板不可搜索:当您使用故事板时,Xcode 中的项目范围搜索并不是真正的项目范围搜索。它们不包含在搜索中。因此,当您从代码中删除自定义类或重命名它时,您必须手动浏览故事板或查看其原始 XML,以确保它与您的代码更改保持一致。不,先生,我不喜欢这样。 更新:在 Xcode 6 中可以搜索 Storyboard。
故事板不太灵活:在代码中,您基本上可以做任何您想做的事情!使用故事板,您只能执行代码中可以执行的操作的一部分。特别是当你想用动画和过渡做一些高级的事情时,你会发现自己在“与故事板作斗争”才能让它工作。
故事板不允许您更改特殊视图控制器的类型:您想将
UITableViewController
更改为UICollectionViewController
吗?或者进入一个普通的 UIViewController ?在故事板中不可能。您必须删除旧的视图控制器并创建一个新的视图控制器并重新连接所有 Segue。在代码中进行这样的更改要容易得多。故事板给您的项目添加了两个额外的负担:(1) 生成故事板 XML 的故事板编辑器工具,以及 (2) 解析 XML 并从中创建 UI 和控制器对象的运行时组件。这两个部分都可能存在您无法修复的错误。
Storyboards 不允许您向
UIImageView
添加子视图:谁知道为什么。故事板不允许您为单个视图(控制器)启用自动布局:通过选中/取消选中故事板中的自动布局选项,更改将应用于故事板中的所有控制器故事板。 (感谢 Sava Mazăre 的这一点!)
Storyboards 破坏向后兼容性的风险较高:Xcode 有时会更改 Storyboard 文件格式,并且不以任何方式保证您能够打开您在几年甚至几个月后今天创建的故事板文件。 (这一点感谢thoughtadvances。请参阅原始评论)
这是麦当劳:说用史蒂夫·乔布斯关于微软的话说:这是麦当劳(视频)!
Storyboards have many more problems than benefits. Here's is a list of their problems, copied from iraycd:
Storyboards fail at runtime, not at compile time: You have a typo in a segue name or connected it wrong in your storyboard? It will blow up at runtime. You use a custom UIViewController subclass that doesn't exist anymore in your storyboard? It will blow up at runtime. If you do such things in code, you will catch them early on, during compile time. Update: My new tool StoryboardLint mostly solves this problem.
Storyboards get confusing fast: As your project grows, your storyboard gets increasingly more difficult to navigate. Also, if multiple view controllers have multiple segues to multiple other view controllers, your storyboard quickly starts to look like a bowl of spaghetti and you'll find yourself zooming in and out and scrolling all over the place to find the view controller you are looking for and to find out what segue points where. Update: This problem can mostly be solved by splitting your Storyboard up into multiple Storyboards, as described in this article by Pilky and this article by Robert Brown.
Storyboards make working in a team harder: Because you usually only have one huge storyboard file for your project, having multiple developers regularly making changes to that one file can be a headache: Changes need to be merged and conflicts resolved. When a conflict occurs, it is hard to tell how to resolve it: Xcode generates the storyboard XML file and it was not really designed with the goal in mind that a human would have to read, let alone edit it.
Storyboards make code reviews hard or nearly impossible: Peer code reviews are a great thing to do on your team. However, when you make changes to a storyboard, it is almost impossible to review these changes with a different developer. All you can pull up is a diff of a huge XML file. Deciphering what really changed and if those changes are correct or if they broke something is really hard.
Storyboards hinder code reuse: In my iOS projects, I usually create a class that contains all the colors and fonts and margins and insets that I use throughout the app to give it a consistent look and feel: It's a one line change if I have to adjust any of those values for the whole app. If you set such values in the storyboard, you duplicate them and will need to find every single occurrence when you want to change them. Chances are high that you miss one, because there's no search and replace in storyboards.
Storyboards make you do everything twice: Are you building a universal app that runs both on iPad and on iPhone? When you use storyboards, you will usually have one storyboard for the iPad version and one for the iPhone version. Keeping both in sync requires you to do every UI or app-workflow change in two places. Yay. Update: In iOS 8 and Xcode 6, you can use a single Storyboard for iPhone and iPad.
Storyboards require constant context switches: I find myself working and navigating much faster in code than in storyboards. When your app uses storyboards, you constantly switch your context: "Oh, I want a tap on this table view cell to load a different view controller. I now have to open up the storyboard, find the right view controller, create a new segue to the other view controller (that I also have to find), give the segue a name, remember that name (I can't use constants or variables in storyboards), switch back to code and hope I don't mistype the name of that segue for my prepareForSegue method. How I wish I could just type those 3 lines of code right here where I am!" No, it's not fun. Switching between code and storyboard (and between keyboard and mouse) gets old fast and slows you down.
Storyboards are hard to refactor: When you refactor your code, you have to make sure it still matches what your storyboard expects. When you move things around in your storyboard, you will only find out at runtime if it still works with your code. It feels to me as if I have to keep two worlds in sync. It feels brittle and discourages change in my humble opinion.
Storyboards are not searchable: A project-wide search in Xcode is not really a project-wide search when you use storyboards. They are not included in the search. So when you remove a custom class from your code or rename it, you will have to manually go through the storyboard or look at its raw XML to make sure it is on par with your code changes. No sir, I don't like it. Update: Storyboards are searchable in Xcode 6.
Storyboards are less flexible: In code, you can basically do anything you want! With storyboards you are limited to a subset of what you can do in code. Especially when you want to do some advanced things with animations and transitions you will find yourself "fighting the storyboard" to get it to work.
Storyboards don't let you change the type of special view controllers: You want to change a
UITableViewController
into aUICollectionViewController
? Or into a plainUIViewController
? Not possible in a Storyboard. You have to delete the old view controller and create a new one and re-connect all the segues. It's much easier to do such a change in code.Storyboards add two extra liabilities to your project: (1) The Storyboard Editor tool that generates the storyboard XML and (2) the runtime component that parses the XML and creates UI and controller objects from it. Both parts can have bugs that you can't fix.
Storyboards don't allow you to add a subview to a
UIImageView
: Who knows why.Storyboards don't allow you to enable Auto Layout for individual View(-Controller)s: By checking/unchecking the Auto Layout option in a Storyboard, the change is applied to ALL controllers in the Storyboard. (Thanks to Sava Mazăre for this point!)
Storyboards have a higher risk of breaking backwards compatibility: Xcode sometimes changes the Storyboard file format and doesn't guarantee in any way that you will be able to open Storyboard files that you create today a few years or even months from now. (Thanks to thoughtadvances for this point. See the original comment)
It's McDonald's: To say it in Steve Jobs' words about Microsoft: It's McDonald's (video)!
在 iOS 7 之前,故事板虽然很简洁,但并不是必须具备的。他们解决的问题和带来的问题一样多。 iOS 7 将天平向 Storyboard 倾斜。
对于 iOS 8 和 9,这不再是问题:使用情节提要!
情节提要的主要缺点是您完全依赖 XCode,并且您最终可能会花费数小时来解决 XCode 错误。但 XCode 已经变得更好了,而且 Storyboard 的优势现在太多了,不容忽视。表视图单元格原型、尺寸类、自动布局支持等。
一些提示:
属于在一起。不要把它当作你整个的宏伟布局
应用。
没关系。
从故事板中所以你所要做的就是让
vc=SomeViewController.create(),该方法处理所有
详细信息(拉故事板,将视图控制器拉出故事板
ETC)。
Prior to iOS 7, Storyboards were kind of neat but not a must have. They introduced as many problems as they solved. iOS 7 tilted the balance towards Storyboards.
With iOS 8 and 9 this is not a question anymore: Use storyboards!
The main downside of storyboard is that you're completely dependent on XCode, and you might end up spending hours spinning your wheels on XCode bugs. But XCode has gotten a lot better, and the advantages with Storyboards are now too numerous to ignore. Table view cell prototypes, Size classes, auto layout support and so on.
Some Tips:
belong together. Don't think of it as a grand layout of your entire
application.
And that's OK.
from the storyboard(s) so all you have to do is let
vc=SomeViewController.create(), where the method handles all the
details (pull story board, pull view controller out of story board
etc).