Xcode中的故事板引用,我们应该在哪里使用它?

时间:2022-09-11 12:10:34

There is one new control in Xcode7 beta named as Storyboard Reference. Below is its image.

Xcode7 beta中有一个新控件,名为Storyboard Reference。下面是其形象。

Xcode中的故事板引用,我们应该在哪里使用它?

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

因此,问题是

  1. In which situations should we use this?
  2. 在什么情况下我们应该使用这个?
  3. Is this used to connect two storyboard's view controllers via segue?
  4. 这是用来连接两个故事板的视图控制器通过segue吗?
  5. Is this approach used to replace VC of another storyboard programatically?
  6. 这种方法是否用于在程序上替换另一个故事板的VC ?
  7. Will it work on older iOS version(before iOS 9)?
  8. 在iOS 9之前,它还能运行吗?

7 个解决方案

#1


47  

UPDATE (January 6, 2016): I just want to quickly mention that using Storyboard references is very simple 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 filename of the individual Storyboard that you want to link to and set the initial view controller within that individual Storyboard. That's it! :)

更新(2016年1月6日):我只想快速地提一下,使用故事板引用非常简单,将帮助您以一种更干净、更可维护的方式使用故事板。它的一个很好的用例是:一个有多个选项卡的UITabBarController。只需为每个选项卡和主界面创建一个故事板。故事板链接到那些单独的故事板使用故事板引用。用法非常简单:创建故事板引用之后,您只需要为它提供要链接到的每个故事板的文件名,并在每个故事板中设置初始视图控制器。就是这样!:)

What follows now is the original answer I gave to @YogeshSuthar's question.

下面是我给@YogeshSuthar问题的原始答案。

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

    这个可以用在这种情况下,您正在使用多个故事板的应用,直到现在你不得不从其他故事板编程实例化视图控制器,好像现在你可以使用这个引用和创建segue故事板就像视图控制器相同的故事板

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

    是的,你将当前故事板中的一个视图控制器与另一个来自不同故事板的视图控制器连接起来你可以在这两个之间创建一个segue

  3. yes, this can be used to replace the code that was formerly used to instantiate view controllers from other storyboards programmatically

    是的,这可以用来替换以前用程序从其他故事板实例化视图控制器的代码

  4. [UPDATE thx to @AlexBasson] Storyboard references can deployed to iOS 8, OS X 10.10 and watchOS 1.

    [将thx更新为@AlexBasson]故事板引用可以部署到iOS 8、OS X 10.10和watchOS 1。

#2


29  

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

我想发布故事板引用的用法它用于通过segue从另一个故事板打开视图控制器而不需要编写任何代码。:)

Steps

  1. First create 1 new storyboard name it anything you want(I named it NewStoryboard).
  2. 首先创建一个新的故事板名称它你想要的任何东西(我命名它为NewStoryboard)。
  3. Place your View controller(or any View Controller) in NewStoryboard.
  4. 在NewStoryboard中放置视图控制器(或任何视图控制器)。
  5. Give it a Storyboard ID(I have given newVC).
  6. 给它一个故事板ID(我给了newVC)。

Xcode中的故事板引用,我们应该在哪里使用它?

  1. Switch back to your main storyboard.
  2. 切换回主故事板。
  3. Drag and drop Storyboard Reference control in this storyboard.
  4. 在这个故事板中拖放故事板引用控件。
  5. Select the Storyboard Reference control and select Attributes Inspector.
  6. 选择故事板引用控件并选择属性检查器。
  7. Select your new storyboard NewStoryboard.
  8. 选择新的storyboard NewStoryboard。
  9. Provide Reference ID as newVC(which you used in NewStoryboard storyboard).
  10. 提供参考ID作为newVC(在NewStoryboard故事板中使用)。
  11. Connect this Storyboard Reference via segue from ViewController. Now it will open this VC via segue without writing code. :)
  12. 从ViewController中通过segue连接这个故事板引用。现在它不用写代码就可以通过segue打开VC。:)

Xcode中的故事板引用,我们应该在哪里使用它?

#3


9  

  1. Will it work on older iOS version(before iOS 9)?
  2. 在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.

故事板引用需要一个至少针对iOS 9.0、OS X 10.11或watchOS的WatchKit 2.0的应用程序。

Source: watchOS Developer Library: Adding a Reference to Another Storyboard

来源:watchOS开发人员库:添加对另一个故事板的引用。

#4


4  

  1. Will it work on older iOS version(before iOS 9)?
  2. 在iOS 9之前,它还能运行吗?

Here is a workaround (verified by Xcode 8 and iOS 8.2):

这里有一个变通方案(由Xcode 8和ios8.2验证):

  1. 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).
  2. 在选项卡的视图控制器中,添加一个容器视图,该视图的框架占据整个视图(如果需要的话,添加约束)。
  3. Delete the embedded view controller that was automatically added by the container view.
  4. 删除由容器视图自动添加的嵌入式视图控制器。
  5. Create your storyboard reference and drag the embed segue from the container view to this reference.
  6. 创建故事板引用并将embed segue从容器视图拖到这个引用。
  7. If you haven't already, create the Storyboard and assign the Storyboard reference to it.
  8. 如果您还没有,创建故事板并为它分配故事板。
  9. 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).
  10. 在新的故事板中,您可以通过选择第一个视图控制器和属性检查器来模拟选项卡栏的外观,在模拟的度量标准下——>底部栏,选择一个选项卡栏来显示(前半透明)。

Xcode中的故事板引用,我们应该在哪里使用它?

#5


3  

Just tested storyboard reference in Xcode 7 with iOS 8 and it failed.

刚刚在Xcode 7中测试了故事板引用和ios8,结果失败了。

Illegal Configuration: Storyboard References cannot be the destinations of 
relationship segues prior to iOS 9.0

#6


3  

On the last iOS Developer Library documentation for Xcode Releases is added this note as well (Current version: Xcode 7.2.1):

在最新的iOS开发人员库文档中,Xcode版本也增加了这条注释(当前版本:Xcode 7.2.1):

Storyboard References may now be deployed to iOS 8, OS X 10.10, and watchOS 1.

故事板引用现在可以部署到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之前的关系segue的目的地

So if you are aware of these two bullet points, then you are good to go :)

因此,如果你意识到这两点,那么你就可以开始了:

#7


2  

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.

当我们有一个包含太多屏幕和模块的大应用程序时,按照每个模块将故事板分成多个故事板是一个很好的和方便的选择。在使用多个故事板时,如果需要在不同故事板的视图控制器之间连接segue,可以使用此控件。如果你用的是这个控件,不需要做编码比如实例化视图控制器和pushViewController等等你只需要连接segue,执行segue如果你需要的话你可以使用prepareForSegue。

Let's discuss your all questions :

让我们来讨论你所有的问题:

  1. In which situations should we use this? - I think you have find your answer above.

    在什么情况下我们应该使用这个?-我想你已经找到了上面的答案。

  2. Is this used to connect two storyboard's view controllers via segue? - YES

    这是用来连接两个故事板的视图控制器通过segue吗?——是的

  3. Is this approach is used to replace VC of another storyboard programmatically? - Nothing to do with coding while using this control

    这种方法是否用于以编程方式替换另一个故事板的VC ?-使用此控件时与编码无关

  4. Will it work on older iOS version(before iOS 9)? - It may work, I have not try it

    在iOS 9之前,它还能运行吗?这可能行得通,但我还没试过

#1


47  

UPDATE (January 6, 2016): I just want to quickly mention that using Storyboard references is very simple 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 filename of the individual Storyboard that you want to link to and set the initial view controller within that individual Storyboard. That's it! :)

更新(2016年1月6日):我只想快速地提一下,使用故事板引用非常简单,将帮助您以一种更干净、更可维护的方式使用故事板。它的一个很好的用例是:一个有多个选项卡的UITabBarController。只需为每个选项卡和主界面创建一个故事板。故事板链接到那些单独的故事板使用故事板引用。用法非常简单:创建故事板引用之后,您只需要为它提供要链接到的每个故事板的文件名,并在每个故事板中设置初始视图控制器。就是这样!:)

What follows now is the original answer I gave to @YogeshSuthar's question.

下面是我给@YogeshSuthar问题的原始答案。

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

    这个可以用在这种情况下,您正在使用多个故事板的应用,直到现在你不得不从其他故事板编程实例化视图控制器,好像现在你可以使用这个引用和创建segue故事板就像视图控制器相同的故事板

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

    是的,你将当前故事板中的一个视图控制器与另一个来自不同故事板的视图控制器连接起来你可以在这两个之间创建一个segue

  3. yes, this can be used to replace the code that was formerly used to instantiate view controllers from other storyboards programmatically

    是的,这可以用来替换以前用程序从其他故事板实例化视图控制器的代码

  4. [UPDATE thx to @AlexBasson] Storyboard references can deployed to iOS 8, OS X 10.10 and watchOS 1.

    [将thx更新为@AlexBasson]故事板引用可以部署到iOS 8、OS X 10.10和watchOS 1。

#2


29  

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

我想发布故事板引用的用法它用于通过segue从另一个故事板打开视图控制器而不需要编写任何代码。:)

Steps

  1. First create 1 new storyboard name it anything you want(I named it NewStoryboard).
  2. 首先创建一个新的故事板名称它你想要的任何东西(我命名它为NewStoryboard)。
  3. Place your View controller(or any View Controller) in NewStoryboard.
  4. 在NewStoryboard中放置视图控制器(或任何视图控制器)。
  5. Give it a Storyboard ID(I have given newVC).
  6. 给它一个故事板ID(我给了newVC)。

Xcode中的故事板引用,我们应该在哪里使用它?

  1. Switch back to your main storyboard.
  2. 切换回主故事板。
  3. Drag and drop Storyboard Reference control in this storyboard.
  4. 在这个故事板中拖放故事板引用控件。
  5. Select the Storyboard Reference control and select Attributes Inspector.
  6. 选择故事板引用控件并选择属性检查器。
  7. Select your new storyboard NewStoryboard.
  8. 选择新的storyboard NewStoryboard。
  9. Provide Reference ID as newVC(which you used in NewStoryboard storyboard).
  10. 提供参考ID作为newVC(在NewStoryboard故事板中使用)。
  11. Connect this Storyboard Reference via segue from ViewController. Now it will open this VC via segue without writing code. :)
  12. 从ViewController中通过segue连接这个故事板引用。现在它不用写代码就可以通过segue打开VC。:)

Xcode中的故事板引用,我们应该在哪里使用它?

#3


9  

  1. Will it work on older iOS version(before iOS 9)?
  2. 在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.

故事板引用需要一个至少针对iOS 9.0、OS X 10.11或watchOS的WatchKit 2.0的应用程序。

Source: watchOS Developer Library: Adding a Reference to Another Storyboard

来源:watchOS开发人员库:添加对另一个故事板的引用。

#4


4  

  1. Will it work on older iOS version(before iOS 9)?
  2. 在iOS 9之前,它还能运行吗?

Here is a workaround (verified by Xcode 8 and iOS 8.2):

这里有一个变通方案(由Xcode 8和ios8.2验证):

  1. 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).
  2. 在选项卡的视图控制器中,添加一个容器视图,该视图的框架占据整个视图(如果需要的话,添加约束)。
  3. Delete the embedded view controller that was automatically added by the container view.
  4. 删除由容器视图自动添加的嵌入式视图控制器。
  5. Create your storyboard reference and drag the embed segue from the container view to this reference.
  6. 创建故事板引用并将embed segue从容器视图拖到这个引用。
  7. If you haven't already, create the Storyboard and assign the Storyboard reference to it.
  8. 如果您还没有,创建故事板并为它分配故事板。
  9. 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).
  10. 在新的故事板中,您可以通过选择第一个视图控制器和属性检查器来模拟选项卡栏的外观,在模拟的度量标准下——>底部栏,选择一个选项卡栏来显示(前半透明)。

Xcode中的故事板引用,我们应该在哪里使用它?

#5


3  

Just tested storyboard reference in Xcode 7 with iOS 8 and it failed.

刚刚在Xcode 7中测试了故事板引用和ios8,结果失败了。

Illegal Configuration: Storyboard References cannot be the destinations of 
relationship segues prior to iOS 9.0

#6


3  

On the last iOS Developer Library documentation for Xcode Releases is added this note as well (Current version: Xcode 7.2.1):

在最新的iOS开发人员库文档中,Xcode版本也增加了这条注释(当前版本:Xcode 7.2.1):

Storyboard References may now be deployed to iOS 8, OS X 10.10, and watchOS 1.

故事板引用现在可以部署到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之前的关系segue的目的地

So if you are aware of these two bullet points, then you are good to go :)

因此,如果你意识到这两点,那么你就可以开始了:

#7


2  

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.

当我们有一个包含太多屏幕和模块的大应用程序时,按照每个模块将故事板分成多个故事板是一个很好的和方便的选择。在使用多个故事板时,如果需要在不同故事板的视图控制器之间连接segue,可以使用此控件。如果你用的是这个控件,不需要做编码比如实例化视图控制器和pushViewController等等你只需要连接segue,执行segue如果你需要的话你可以使用prepareForSegue。

Let's discuss your all questions :

让我们来讨论你所有的问题:

  1. In which situations should we use this? - I think you have find your answer above.

    在什么情况下我们应该使用这个?-我想你已经找到了上面的答案。

  2. Is this used to connect two storyboard's view controllers via segue? - YES

    这是用来连接两个故事板的视图控制器通过segue吗?——是的

  3. Is this approach is used to replace VC of another storyboard programmatically? - Nothing to do with coding while using this control

    这种方法是否用于以编程方式替换另一个故事板的VC ?-使用此控件时与编码无关

  4. Will it work on older iOS version(before iOS 9)? - It may work, I have not try it

    在iOS 9之前,它还能运行吗?这可能行得通,但我还没试过