Storyboard Reference in Xcode, Where Should We Use It

Storyboard reference in Xcode, where should we use it?

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.

How to add storyboard reference in another storyboard

Interface Builder in Xcode 7 has "Storyboard Reference" object in the "Object library":

Sample Image

Just drag that onto your storyboard. You can then select that storyboard reference and specify the storyboard to which it refers in the attributes inspector:

Sample Image

Then you can now add segue to this reference and you'll segue from the scene in one storyboard to a scene in another storyboard.

iOS: Main.storyboard references the initial view controller

This is a storyboard reference, pointing to another storyboard named newStoryBoard. For the segue to work, it needs to know which view controller it should open from that storyboard.

Open newStoryBoard.storyboard, select the appropriate view controller, and check the Is Initial View Controller box in the properties panel on the right:

checkbox

Alternatively, you can set the Referenced Id in the storyboard reference properties. Set it to the storyboard Id of the view controller you want to open, and you should be all set.

Using Storyboard Reference

if you want to reference two storyboards in Xcode 7, in your initial storyboard select the view controllers you want to use in the second storyboard, in the task bar select "Editor" and then "Refactor to Storyboard", then you have to name the new storyboard and thats all, i don´t know if you can reference to an .xib file, notice that you can use the storyboard reference to call a view even in the same storyboard or even a view that is not connected to any other view, and in the reference you can specify which view you want to load (you do this in the identity inspector), or leave that space empty and the view loaded will be the initial view.

Better explained step by step:

If you want to create the new storyboard at the same time

  • select the views you want to move to the new storyboard
  • open the Editor's menu and select Refactor to Storyboard
  • Xcode will ask you the name you want to give to the new storyboard
    and thats all

If you have already created the storyboard

  • Open the storyboard where you want to call the second storyboard
  • drag the Storyboard Reference that is located in the objects library
  • select the reference you added and open the identity inspector
  • in the identity inspector write the name of the storyboard that you want
    to call (required) and the storyboardID of the view you want to load
    (optional)

thats how you use storyboard references in Xcode 7, hope that is useful to you

Should Storyboard References work with Container Views?

Setting up a parent/child relationship with a container view is easy.Just create a container view inside the parent ViewController, create the child view controller scene, and then control-drag from the container view to the child view controller to create the embed segue.

If you want to swap the child view controllers depending on some condition, i.e. multiple child view controllers, just create a custom segue. This custom segue is named Empty and is a subclass of UIStoryboardSegue with empty perform method.



Related Topics



Leave a reply



Submit