ios Xcode 中的 Storyboard 参考,我们应该在哪里使用它?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30772145/
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
Storyboard reference in Xcode, where should we use it?
提问by Yogesh Suthar
There is one new control in Xcode7 beta
named as Storyboard Reference
. Below is its image.
有一个Xcode7 beta
名为 as 的新控件Storyboard Reference
。下面是它的图像。
It has its description as
它的描述为
Provides a placeholder for a view controller in an external storyboard. Segues connected to this placeholder will instantiate the referenced view controller at runtime.
为外部故事板中的视图控制器提供占位符。连接到此占位符的 Segue 将在运行时实例化引用的视图控制器。
So the questions are
所以问题是
- In which situations should we use this?
- Is this used to connect two storyboard's view controllers via segue?
- Is this approach used to replace VC of another storyboard programatically?
- Will it work on older iOS version(before iOS 9)?
- 我们应该在哪些情况下使用它?
- 这是否用于通过 segue 连接两个故事板的视图控制器?
- 这种方法是否用于以编程方式替换另一个故事板的 VC?
- 它适用于较旧的 iOS 版本(iOS 9 之前)吗?
采纳答案by nburk
UPDATE (January 6, 2016): I just want to quickly mention that using Storyboard references is verysimple and is going to help you use Storyboards in a much more clean and maintainable way. A good use case for it is e.g. a UITabBarController
with multiple tabs. Just create one Storyboard for each tab and in your Main.Storyboard
link to those individual Storyboards using Storyboard references.Usage is very straightforward: after creating a Storyboard reference you only need to give it the filenameof the individual Storyboard that you want to link to and set the initial view controllerwithin that individual Storyboard. That's it! :)
更新(2016 年 1 月 6 日):我只想快速提及使用 Storyboard 引用非常简单,它将帮助您以更干净和可维护的方式使用 Storyboard。它的一个很好的用例是例如UITabBarController
具有多个选项卡。只需为每个选项卡创建一个故事板,并Main.Storyboard
使用故事板引用在指向这些单个故事板的链接中创建一个故事板。用法非常简单:在创建 Storyboard 引用后,您只需为其指定要链接到的单个 Storyboard的文件名,并在该单个 Storyboard 中设置初始视图控制器。就是这样!:)
What follows now is the original answerI gave to @YogeshSuthar's question.
现在接下来是我对@YogeshSuthar 的问题给出的原始答案。
this can be used in cases where you are using multiple storyboards in your app. until now you'd have to instantiate view controllers from other storyboards programmatically, seems like now you can just use this reference and create your segue in the storyboards just like with view controllers from the same storyboard
yes, you connect one view controller from your current storyboard with another view controller from a different storyboard and you can create a segue between these two
yes, this can be used to replace the code that was formerly used to instantiate view controllers from other storyboards programmatically
[UPDATE thx to @AlexBasson] Storyboard references can deployed to iOS 8, OS X 10.10and watchOS 1.
这可以用于您在应用程序中使用多个故事板的情况。到目前为止,您必须以编程方式从其他故事板中实例化视图控制器,似乎现在您可以使用此参考并在故事板中创建您的转场,就像使用来自同一故事板的视图控制器一样
是的,您将当前故事板中的一个视图控制器与另一个故事板中的另一个视图控制器连接起来,您可以在这两者之间创建一个segue
是的,这可用于替换以前用于以编程方式从其他故事板实例化视图控制器的代码
[更新感谢@AlexBasson] Storyboard 引用可以部署到iOS 8、OS X 10.10和watchOS 1。
回答by Yogesh Suthar
Usage of Storyboard Reference
故事板参考的使用
Well other answer's are correct to my questions.
好吧,其他答案对我的问题是正确的。
And I want to post the usage of the Storyboard Reference
which is used to open View controllers from another storyboard via segue without writing any code. :)
我想发布Storyboard Reference
用于通过 segue 从另一个故事板打开视图控制器的 的用法,而无需编写任何代码。:)
Steps
脚步
- First create 1 new storyboard name it anything you want(I named it
NewStoryboard
). - Place your View controller(or any View Controller) in
NewStoryboard
. - Give it a
Storyboard ID
(I have givennewVC
).
- 首先创建 1 个新的故事板,将其命名为您想要的任何名称(我将其命名为
NewStoryboard
)。 - 将您的视图控制器(或任何视图控制器)放在
NewStoryboard
. - 给它一个
Storyboard ID
(我已经给了newVC
)。
- Switch back to your main storyboard.
- Drag and drop
Storyboard Reference
control in this storyboard. - Select the
Storyboard Reference
control and selectAttributes Inspector
. - Select your new storyboard
NewStoryboard
. - Provide Reference ID as
newVC
(which you used inNewStoryboard
storyboard). - Connect this
Storyboard Reference
via segue from ViewController. Now it will open this VC via segue without writing code. :)
- 切换回主故事板。
Storyboard Reference
在这个故事板中拖放控件。- 选择
Storyboard Reference
控件并选择Attributes Inspector
。 - 选择您的新故事板
NewStoryboard
。 - 提供参考 ID
newVC
(您在NewStoryboard
故事板中使用的)。 Storyboard Reference
通过来自 ViewController 的 segue连接它。现在它将通过 segue 打开这个 VC,而无需编写代码。:)
回答by Ben
- Will it work on older iOS version(before iOS 9)?
- 它适用于较旧的 iOS 版本(iOS 9 之前)吗?
Looks like maybe not :/
看起来可能不是:/
Storyboard references required an app targeting at least iOS 9.0, OS X 10.11, or WatchKit 2.0 for watchOS.
Storyboard 引用要求应用至少针对 iOS 9.0、OS X 10.11 或 WatchKit 2.0 for watchOS。
Source: watchOS Developer Library: Adding a Reference to Another Storyboard
回答by Nianliang
- Will it work on older iOS version(before iOS 9)?
- 它适用于较旧的 iOS 版本(iOS 9 之前)吗?
Here is a workaround (verified by Xcode 8 and iOS 8.2):
这是一个解决方法(由 Xcode 8 和 iOS 8.2 验证):
- In the view controller for the tab, add a container view whose frame takes up the entire view (add constraints to do this if necessary).
- Delete the embedded view controller that was automatically added by the container view.
- Create your storyboard reference and drag the embed segue from the container view to this reference.
- If you haven't already, create the Storyboard and assign the Storyboard reference to it.
- In the new Storyboard, you can simulate the appearance of the tab bar by selecting the first View Controller and in the Attributes Inspector, under Simulated Metrics -> Bottom Bar, selecting a Tab Bar to show (ex. Translucent).
- 在选项卡的视图控制器中,添加一个容器视图,其框架占据整个视图(如有必要,添加约束来执行此操作)。
- 删除由容器视图自动添加的嵌入式视图控制器。
- 创建您的故事板参考并将嵌入转场从容器视图拖动到此参考。
- 如果您还没有,请创建 Storyboard 并将 Storyboard 引用分配给它。
- 在新的 Storyboard 中,您可以通过选择第一个 View Controller 并在 Attributes Inspector 中的 Simulated Metrics -> Bottom Bar 下选择要显示的 Tab Bar(例如半透明)来模拟 Tab Bar 的外观。
回答by yangt
Just tested storyboard reference in Xcode 7 with iOS 8 and it failed.
刚刚使用 iOS 8 在 Xcode 7 中测试了故事板参考,但失败了。
Illegal Configuration: Storyboard References cannot be the destinations of
relationship segues prior to iOS 9.0
回答by E-Riddie
On the last iOS Developer Librarydocumentation for Xcode Releasesis added this note as well (Current version: Xcode 7.2.1):
在Xcode Releases的最后一个iOS Developer Library文档中也添加了这个注释(当前版本:Xcode 7.2.1):
Storyboard References may now be deployed to iOS 8, OS X 10.10, and watchOS 1.
Storyboard References 现在可以部署到 iOS 8、OS X 10.10 和 watchOS 1。
That means good news but still keep in mind that:
这意味着好消息,但请记住:
Storyboard References cannot be the destinations of relationship segues prior to iOS 9.0
在 iOS 9.0 之前,Storyboard References 不能作为关系转场的目的地
So if you are aware of these two bullet points, then you are good to go :)
因此,如果您了解这两个要点,那么您就可以开始了:)
回答by Nirav Gadhiya
When we have a big application with too many screens and modules, Dividing storyboard into multiple storyboards as per modules is a good and convenient option. While using multiple storyboard, if you need to connect segue between viewControllers of different storyboard, you can use this control. If you are using this control, No need to do coding stuffs like instantiateViewController and pushViewController etc. You can just connect the segue, perform the segue and if you need you can use prepareForSegue.
当我们有一个包含太多屏幕和模块的大型应用程序时,将故事板按模块划分为多个故事板是一个很好且方便的选择。在使用多个storyboard时,如果需要在不同storyboard的viewController之间连接segue,可以使用这个控件。如果您正在使用此控件,则无需编写诸如 instantiateViewController 和 pushViewController 之类的代码。您只需连接 segue,执行 segue,如果需要,您可以使用 prepareForSegue。
Let's discuss your all questions :
让我们讨论您的所有问题:
In which situations should we use this? - I think you have find your answer above.
Is this used to connect two storyboard's view controllers via segue? - YES
Is this approach is used to replace VC of another storyboard programmatically? - Nothing to do with coding while using this control
Will it work on older iOS version(before iOS 9)? - It may work, I have not try it
我们应该在哪些情况下使用它?-我想你已经在上面找到了答案。
这是否用于通过 segue 连接两个故事板的视图控制器?-是的
这种方法是否用于以编程方式替换另一个故事板的 VC?-使用此控件时与编码无关
它适用于较旧的 iOS 版本(iOS 9 之前)吗?-可能有用,我没试过