在 iOS 编程中使用 Storyboards 而不是 xib 文件有什么好处?

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

What are the benefits of using Storyboards instead of xib files in iOS programming?

iosxcodestoryboardinterface-builderxib

提问by Steve

What are the main differencesbetween using Storyboards and xib files.

使用 Storyboards 和 xib 文件之间的主要区别是什么。

Specifically, what are the advantages or disadvantagesof using a Storyboard?

具体来说,使用 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.

不幸的是,尽管做了很多研究,我在 Storyboards 上找到的只是简单的教程,向您展示如何设置 Storyboard,而不是解释它们是什么的具体信息。

回答by LJ Wilson

A Storyboard is:

故事板是:

  • A container for all your Scenes (View Controllers, Nav Controllers, TabBar Controllers, etc)
  • A manager of connections and transitions between these scenes (these are called Segues)
  • A nice way to manage how different controllers talk to each other
  • Storyboards give you a complete look at the flow of your application that you can never get from individual nib files floating around.
  • A reducer of all the "clutter" that happens when you have several controllers each with it's own nib file.
  • 所有场景的容器(视图控制器、导航控制器、TabBar 控制器等)
  • 这些场景之间的连接和转换管理器(这些被称为 Segues)
  • 管理不同控制器如何相互通信的好方法
  • 故事板让您可以完整地了解应用程序的流程,而您永远无法从浮动的单个 nib 文件中获得这些信息。
  • 当您有多个控制器时,每个控制器都有自己的 nib 文件时发生的所有“混乱”的减速器。

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.

我已经使用 Storyboards 一段时间了,唯一的缺点是你不能针对 iOS 4 或更低版本。故事板仅适用于运行 iOS 5 或更高版本的设备。除此之外,好处很多,缺点是 IMO 不存在。

The best tutorial I have seen is Ray Wenderlich's

我见过的最好的教程是Ray Wenderlich 的

Also, if you are a member of the Apple Developer program, check out last years WWDC session on Storyboards (iTunesU), it is awesome.

此外,如果您是 Apple 开发人员计划的成员,请查看去年关于 Storyboards (iTunesU) 的 WWDC 会议,这很棒。

Another great one (also on iTunesU) is the latest Stanford iOS Application Programming course.

另一个很棒的课程(也在 iTunesU 上)是最新的斯坦福 iOS 应用程序编程课程。

回答by brainray

There are not only pro sides of Storyboarding, also cons - just because you asked for input:

故事板不仅有优点,也有缺点——仅仅因为你要求输入:

  • it's not easy to work with SBs in a team, since only one participant can work on the SB at once (because it's one file).
  • 在团队中与 SB 一起工作并不容易,因为一次只有一个参与者可以处理 SB(因为它是一个文件)。

-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)

- 以下是不正确的: - 如果您需要做 SB 不提供的事情,将 SB 与编程创建的视图混合起来并不容易(好吧,虽然有可能)

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.

经验法则似乎是:你期望你的项目越复杂,你就越不选择 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

编辑: - SB 的另一个缺点:解决所有有关 SB 的 XCode 烦人的错误。例如,由于一些不一致,必须经常刷新 DerivedData 文件夹。有时故事板文件或指向它们的链接会损坏。那么你可能会很高兴去寻找问题。看看这个线程以获得想法

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.

编辑 2(2013 年 3 月):与此同时,Storyboards 和 Xcode 工作得更好,文档和最佳实践广泛传播。我认为大多数项目都可以推荐使用故事板,即使仍然存在一些故障。

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.

编辑 3(2013 年 9 月):现在使用新的 Xcode 5 格式与 SB 合作可能会变得更好,因为现在似乎可以更容易合并 SB 代码。

Another EDIT: well, if you have an hour of time, sit back, relax and listen to these guys discussing this topic (Ray Wenderlich & Co)

另一个编辑:好吧,如果你有一个小时的时间,坐下来,放松一下,听听这些人讨论这个话题(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.

编辑 2016.1:在长期担任 Storyboard 倡导者之后,过去几个月我遇到了很多麻烦,我决定尽可能放弃 Storyboard。原因是苹果增加了愚蠢的功能,但并不关心错误和缺陷。具有大量自动布局约束的性能非常糟糕(在设计时),并且容易出错。示例:在 Xcode 中打开项目后,即使不太复杂的 Storyboards 往往会进入“脏模式”(请参阅​​ git state)。提示:作为初学者,您会喜欢 Storyboards,因为您可以快速创建原型并在没有大量代码的情况下运行。当您进入中间状态时,您将向项目添加更多 GUI 代码。现在你开始在代码和 SB 之间来回走动——事情开始变得更糟。

回答by Kevin

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).

Nibs/.xib 文件和 Storyboards 都是 Interface Builder 文件,用于在 Xcode 中为 iOS 和 Mac 应用程序可视化创建用户界面(我将使用 iOS 术语作为类,因为这个问题被标记为 iOS 但它也适用于 Mac 编程) .

Differences

差异

Nibs are intended to be used with a single UIView. They can also be connected to a UIViewControllersubclass by settings the class of File's Owner to any subclass of UIViewControllerand connection the view outlet (drag to connect using the Connections Inspector in the far right pane of Xcode).

笔尖旨在与单个UIView. 它们还可以通过将UIViewControllerFile's Owner的类设置为任何子类UIViewController并连接视图插座来连接到子类(使用 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.

故事板旨在包含 1 个或多个UIViewController. 您可以在单个故事板中构建整个用户界面,也可以将其分成更小的部分。

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.

应始终使用故事板来支持 .xib 文件/Nib(用于视图控制器)。故事板具有更多功能,并且由 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 UIViewControllerjust as easily as you can with Nibs (see code samples below). Keep reading for a detailed explanation and code examples.

每个支持 Nib 的论点都依赖于它们单独使用而故事板包含许多场景的事实。您可以UIViewController像使用 Nib 一样轻松地为每个故事板使用单个故事板(请参阅下面的代码示例)。继续阅读以获取详细说明和代码示例。

Detailed

详细的

Why are Storboards superior to Nibs?

为什么 Storboards 优于 Nibs?

The answer basically comes down to Apple encouraging the use of Storyboards and putting more development effort into them.

答案基本上归结为 Apple 鼓励使用 Storyboards 并在其中投入更多的开发工作。

  1. Storyboards have zooming capability which Nibs lack. Seriously, you can't zoom at all in Nibs which sucks when designing for bigger screens on a small laptop.
  2. Nibs are missing key functionality like:
    • Prototype and dynamic cells for UITableView(more info)
    • The top layout guideproperty (see comment)
    • There are probably more, please edit or comment if you have something to add to this list
  3. You don't need to mess with setting the class of Files Owner.
  1. 故事板具有 Nib 所缺乏的缩放功能。说真的,你根本无法放大 Nibs,这在为小型笔记本电脑设计更大屏幕时很糟糕。
  2. Nib 缺少关键功能,例如:
    • UITableView更多信息)的原型和动态单元格
    • 顶部布局引导物业(见注释)
    • 可能还有更多,如果您有要添加到此列表中的内容,请进行编辑或评论
  3. 您不需要设置文件所有者的类。

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.

反对故事板的基本论点是,将所有视图控制器放在一个地方会导致合并冲突、缓慢的 Xcode、缓慢的构建时间以及维护的普遍痛苦。因此,一般建议是为每个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.

但是...您可以为每个UIViewController. 一个常见的做法(至少对我来说)是在一个类方法中隐藏所有 UIViewController 初始化(因为没有其他类需要知道控制器的 Nib/Storyboard 所在的文件的名称)。让我们比较可能用于创建此类方法的相关代码片段。一行代码就是两者之间的全部区别。

Objective-C

目标-C

Storyboard

故事板

+ (ViewController *)create
{
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"ViewController" bundle:nil];
    return [storyboard instantiateInitialViewController];
}

Nib

笔尖

+ (ViewController *)create
{
    return [super initWithNibName:@"ViewController" bundle:nil];
}

Usage

用法

- (void)showMyViewController
{
    ViewController *vc = [ViewController create];
    [self presentViewController:vc animated:YES completion:nil];
}

Swift

迅速

Storyboard

故事板

static func create() -> ViewController {
    let storyboard = UIStoryboard(name: "ViewController", bundle: NSBundle.mainBundle())
    return storyboard.instantiateInitialViewController() as! ViewController
}

Nib

笔尖

static func create() -> ViewController {
    return ViewController(nibName: "ViewController", bundle: nil)
}

Usage

用法

func showMyViewController() {
    let vc = ViewController.create()
    self.presentViewController(vc, animated: true, completion: nil)
}

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

我将解决 Nibs 的所有常见论点;正如我之前提到的,大多数支持单个文件,而不是作为 Nibs 而非 Storyboards 的论据

  1. Teams and merging
  1. 团队和合并

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

响应:单个故事板不会比单个 Nib 引起更多的合并冲突

  1. Complexity
  1. 复杂

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.

论点:非常复杂的应用程序在 Storyboard 中有很多场景,这导致了一个巨大的 Storyboard,它需要永远加载并且由于它的大小而几乎无法理解。

Response: This is a great point, but you can easily break Storyboards into smaller parts. Storyboard Referenceslook 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.

回应:这是一个很好的观点,但您可以轻松地将 Storyboard 分成更小的部分。Storyboard References看起来是一个很棒的功能,可用于将 Storyboard 链接在一起,但它们仅在 Xcode 7/iOS 9+ 中可用。此外,仍然不是选择单个笔尖而不是故事板的理由。

  1. Reuseability
  1. 可重用性

Argument: Creating a Nib for each UIViewControllersubclass lets you reuse code so you don't have to setup all your constraints and outlets for each scene in your storyboard.

论点:为每个UIViewController子类创建一个 Nib可以让您重用代码,这样您就不必为故事板中的每个场景设置所有约束和出口。

Response: Again, not a reason to choose individual Nibs over individual Storyboards.

回应:同样,这不是选择单个 Nib 而非单个 Storyboard 的理由。

回答by railwayparade

Some more benefits of storyboards:

故事板的更多好处:

  • Storyboards have better support for tableviews. That is you can use "Dynamic" and "Prototype" cells.
  • It is easier to instantiate view controllers using storyboards. You can do stuff like: [se lf.storyboard instantiateViewControllerWithIdentifer:]
  • Storyboards support view controller containers, so you can have child view controllers layed out graphically.
  • 故事板对表格视图有更好的支持。也就是说,您可以使用“动态”和“原型”单元格。
  • 使用故事板实例化视图控制器更容易。您可以执行以下操作: [se lf.storyboard 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 包含大量视图控制器时,在 XCode 中渲染速度很慢

  • 无法为故事板中的一个视图控制器启用自动布局。

回答by Stephen Darlington

There was a nice presentation about Storyboardgiven at the LiDG meeting a couple of months ago.

几个月前在 LiDG 会议上有一个关于 Storyboard精彩演讲

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.

就个人而言,我会说这是使用新应用程序的方式。有一些差距,特别是对于非常复杂的应用程序,但专业人士大多超过了缺点。

回答by Beleg

Be careful, if you use Storyboards your app is not backwards compatible with older OS installations.

请注意,如果您使用 Storyboards,您的应用程序无法向后兼容旧的操作系统安装。

回答by Ell Neal

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.

故事板基本上是一种使您作为开发人员的工作更轻松的设备。它被编译成一系列 nib 文件,因此性能几乎相当,但是作为开发人员,能够快速查看整个应用程序流程的概览是很棒的。

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.

我开始过渡到在新项目中使用故事板,前提是我可以说服客户接受 iOS 5 作为最低版本。这纯粹是因为我更喜欢这样做,而且完成相同任务所需的时间更少。

回答by Sandeep Rajbhar

Advantages:

好处:

1) It's very nice to design interfaces

1)界面设计的很好

2) You can use StoryBoard Segues to identify navigation/modal relationships in a cool manner.

2) 您可以使用 StoryBoard Segues 以一种很酷的方式识别导航/模式关系。

3) If your app supports multiple devices, it's a good way to organize different views.

3) 如果您的应用支持多种设备,这是组织不同视图的好方法。

4) Prototyping is another added advantage.

4) 原型制作是另一个额外的优势。

5) Prototype UITableViewCell can save time and it reduce the amount of the code too.

5) 原型 UITableViewCell 可以节省时间,也减少了代码量。

6) you can see all the screens of the app at one place by using StoryBoard.

6) 使用 StoryBoard,您可以在一处查看应用程序的所有屏幕。

7) You can easily view the relationship among them

7)您可以轻松查看它们之间的关系

8) if you are working on someone's code you can get the better understanding of the flow of the app.

8) 如果您正在处理某人的代码,您可以更好地了解应用程序的流程。

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.

9) 您可以通过应用情节提要中的视网膜外形为 iPhone 4 和 iPhone 5 设置用户界面,而无需一次又一次地运行应用程序。

10) Clients can see the prototype of the app before start developing it, here storyboard helps you a lot.

10)客户可以在开始开发之前看到应用程序的原型,这里故事板对你有很大帮助。

Disadvantages:

缺点:

1) It is only available in iOS 5+

1) 仅适用于 iOS 5+

2) StoryBoardSegues are kind of rigid and you may make use of prepareForSegue many times.

2) StoryBoardSegues 有点死板,您可能会多次使用 prepareForSegue。

4) Like IB, not very friendly with other display engines and toolkits.

4)和IB一样,对其他显示引擎和工具包不太友好。

4) Makes it hard to share designs for a single view or set of views - you have to send all or nothing.

4) 难以共享单个视图或一组视图的设计——您必须全部发送或不发送。

5) For storyboard you will need a big screen specially in case of iPad.

5) 对于故事板,您需要一个大屏幕,特别是在 iPad 的情况下。

6) Difficulty while copying views from other apps to storyboard.

6) 将视图从其他应用程序复制到故事板时遇到困难。

7) Problems in storyboard when multiple developers work on the same project by using git repository

7) 多个开发者在同一个项目上使用 git repository 时 storyboard 的问题

copied from some resource

从某些资源复制

回答by Drew C

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.

您对 Auto Layout 的态度也可能会影响您是否要使用 Storyboard。使用 xib,您可以在每个 .xib 的基础上启用或禁用自动布局,允许在您的应用程序中进行混合,而 Storyboards 将您的选择应用于它们包含的所有视图。

回答by Ricardo

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.

您可以在一秒钟内看到大图。拥有许多 NIB 文件,您看不到大局。更容易维护您的程序。更容易理解其他程序......等等。