Storyboard reference in Xcode, where should we use it?
Asked Answered
C

7

66

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

enter image description here

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.

So the questions are

  1. In which situations should we use this?
  2. Is this used to connect two storyboard's view controllers via segue?
  3. Is this approach used to replace VC of another storyboard programatically?
  4. Will it work on older iOS version(before iOS 9)?
Cards answered 11/6, 2015 at 5:19 Comment(2)
is the situation cleared up for you or do you need more explanation? :) @YogeshSutharMalodorous
@Malodorous Yeah cleared up, and I found how to use it. I posted it as answer too. :)Cards
M
51

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

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

  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

  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

  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.

Malodorous answered 11/6, 2015 at 5:29 Comment(5)
Replace means we don't have to write code to open VC from different storyboard. Well your answers are satisfactory, but I am still waiting for more detailed answer. +1Cards
According to the Xcode 7 Beta 4 release notes, "Storyboard references may be deployed to iOS 8, OS X 10.10, and watchOS 1." (adcdownload.apple.com/Developer_Tools/Xcode_7_beta_4/…)Maggard
thanks for the hint, I update the answer! @AlexBassonMalodorous
It's worth a note that on iOS8 Storyboard Reference can't be a relationship segue i.e. from tabbar to view controllerBasra
How would one use an unwind segue?Narvaez
C
39

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

Steps

  1. First create 1 new storyboard name it anything you want(I named it NewStoryboard).
  2. Place your View controller(or any View Controller) in NewStoryboard.
  3. Give it a Storyboard ID(I have given newVC).

enter image description here

  1. Switch back to your main storyboard.
  2. Drag and drop Storyboard Reference control in this storyboard.
  3. Select the Storyboard Reference control and select Attributes Inspector.
  4. Select your new storyboard NewStoryboard.
  5. Provide Reference ID as newVC(which you used in NewStoryboard storyboard).
  6. Connect this Storyboard Reference via segue from ViewController. Now it will open this VC via segue without writing code. :)

enter image description here

Cards answered 11/6, 2015 at 6:2 Comment(1)
You saved my world like Tony Stark. Exactly the steps for Xcode 10.Shortsighted
I
9
  1. Will it work on older iOS version(before 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.

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

Indubitability answered 14/6, 2015 at 1:21 Comment(2)
This has changed as of Xcode 7 Beta 4. According to the release notes, "Storyboard references may be deployed to iOS 8, OS X 10.10, and watchOS 1." (adcdownload.apple.com/Developer_Tools/Xcode_7_beta_4/…)Maggard
You can use xCode7 or latest. Storyboard References may now be deployed to iOS 8, OS X 10.10, and watchOS 1. Backwards-deployed Storyboard References may not be connected to relationship segues and may not refer to storyboards in external bundles. (21275172) developer.apple.com/library/tvos/releasenotes/DeveloperTools/…Dumdum
E
6
  1. Will it work on older iOS version(before iOS 9)?

Here is a workaround (verified by Xcode 8 and iOS 8.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. Delete the embedded view controller that was automatically added by the container view.
  3. Create your storyboard reference and drag the embed segue from the container view to this reference.
  4. If you haven't already, create the Storyboard and assign the Storyboard reference to it.
  5. 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).

screenshot

Ejaculatory answered 18/10, 2016 at 18:17 Comment(4)
This is pretty good for most purposes, but one drawback I found is that it seems to be impossible to hide the tab bar from within the embedded Storyboard.Mineraloid
@Mineraloid get the reference of the view controller which included container view by override - (void)didMoveToParentViewController:(UIViewController *)parent then hide it by parent.tabBarController.tabBar.hidden = YES; see https://mcmap.net/q/17081/-from-within-a-view-controller-in-a-container-view-how-do-you-access-the-view-controller-containing-the-containerEjaculatory
That's true. I guess my problem is that the tab bar hides without any animation instead of moving off screen like with pushing a status bar with hidesBottomBarWhenPushed enabled.Mineraloid
How to pass data to through storyboard reference?Grimaldi
F
3

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

Illegal Configuration: Storyboard References cannot be the destinations of 
relationship segues prior to iOS 9.0
Frizzell answered 24/9, 2015 at 2:43 Comment(5)
This is specifically mentioned in the change log for Xcode 7.0. Backwards compatible relationship segues (ie. compatible with iOS 8) can't be relationship segues (ie. view controllers in tab bar controller). Normal segues (show/present segue etc.) work as expected with storyboard reference on iOS 8.Loriloria
This feature is completely useless then, all segues from a UITabBarController are relationshipsClung
Indeed, this is quite useless. I think that compiler could easily inject code for this to work on iOS8.Extracanonical
@CristiBăluță A workaround is wrapping the destination with container view(embed segue) in a generic view controller. See my answer for more.Ejaculatory
I suppose nobody's supporting ios7 anymore so shouldn't be a problem, but i believe none should use this feature either, nor segues. Who knows what brilliant ideas they'll get in the future... I believe wireframes is the way to go this days and you can't fail with them in the future either.Clung
G
3

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

Storyboard References may now be deployed to iOS 8, OS X 10.10, and 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

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

Garda answered 11/2, 2016 at 14:8 Comment(1)
Yeah we can deploy to iOS8 too. :)Cards
B
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.

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

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

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

Bearskin answered 11/6, 2015 at 5:35 Comment(4)
Nice. But, as the latest news "Storyboard references can deployed to iOS 8, OS X 10.10 and watchOS 1". It says it can be deployed. But, can we compile it under iOS 8 target?Rayburn
Yes you can compile it for iOS8 target. I did and i am using xCode7.1Dumdum
@AkshitZaveri : Note that Storyboard References cannot be the destinations of relationship segues prior to iOS 9.0 . All other segues work fine for iOS 8.Fastidious
You don't instantiate vc and push it but you do something rather more complicated, implement the confusing prepareForSegue.Clung

© 2022 - 2024 — McMap. All rights reserved.