Storyboard Reference, storyboard

Source: Internet
Author: User

Storyboard Reference, storyboard

In some cases, your applications may contain a variety of complex interfaces. If you use Storyboard to manage these interfaces, generally, you will divide these interfaces into many sub-modules according to the corresponding logic and put them in different storyboard, such as registration-related sub-modules in Register. storyboard. Save the image to Photos. storyboard, and the Main interface is placed in Main. storbyoard. This not only reduces the opening speed of each storbyoard, but also enhances maintainability and makes the interface logic clearer.

We recommend that you do what you can do in Storyboard as much as possible to reduce the amount of code. Before iOS9, Storyboard communication (Segue) can only be implemented by code.

For example, we want to reference Login. Storyboard in Main. Storyboard:

Let loginStoryboard = UIStoryboard (name: "Login", bundle: nil) // Login is the name of the corresponding Storyboard, without the suffix. Let loginViewController = loginStoryboard. instantiateViewControllerWithIdentifier ("LoginInterface") // obtain the ViewController In the storyboard Based on the StoryboardID stated in the storyboard

First, we pass the name of the Storyboard file to be referenced (Login) to the UIStoryboard constructor, and then call the instantiateViewControllerWithIdentifier method in the obtained loginStoryboard to obtain the corresponding ViewController.

The good news is that Storyboard Reference is introduced in Xcode7 to replace the above Code. The following is a simple example to illustrate how to use the Storyboard Reference.


First assume that you have downloaded the Xcode7-beta version and created a new Single View Application, the template will automatically create a Main. storyboard file, and the file contains a ViewController Scene by default. Create another storyboad in the project and name it Second. storyboard. Enable Second. storyboard file, drag a ViewController in the Object Library, select "Is Initial View Controller" in the attribute panel, and set the View background color in the view Controller to Blue to show the difference. :



Return to the Main. storyboard file and drag a Button to the only ViewController. Now we want to jump to the ViewController in Second. storyboard by clicking the Button. What should we do? After carefully observing the Object Library, we will find an additional Storyboard Reference ,:



This Storyboard Reference represents the Reference of the Storyboard file. You can configure this Storyboard Reference through Attribute Inspector.


The Storyboard Reference attribute panel has three fields. The first field (Storyboard) specifies the name of the Storyboard file to be referenced, and the second field specifies the ViewController to jump, this ID is the Storyboard ID of ViewController in Storyboard.



If this parameter is not specified, the Initial View Controller of the Storyboard is used by default. The third field specifies the Bundle in which the Storyboard exists. For example, if it comes from another Framework, you can specify the bundle of the Framework. If it exists in the same project, leave it empty.


Now in Main. drag a storyboard Reference in the Storyboard to the canvas, click the Storyboard Reference, select the attribute panel in the right panel, select Second for the Storyboard field, and leave the Reference ID blank (use Initial View Controller ), bundle is not required. At this time, the Storyboard Reference has been configured ,:



In the Main. storyboard file, hold down the Ctrl key and drag the Button to the Storyboard Reference. In the displayed dialog box, select Present Modally.



All right, you can see that the View Controller from Second. storyboard is successfully displayed by building and Run and clicking the button. (There is only a bug that cannot be returned after the blue interface pops up ...)


In addition, if the previous project only has one Storyboard file, you can reconstruct the Storyboard according to the corresponding logic. You only need to select the View Controllers You Want to Refactor, and then click the menu Editor-> Refactor to Storyboard, in the pop-up dialog box, enter the new Storyboard file name and click "OK". All the View Controllers you selected will be placed in the new Storyboard file, in addition, the configured Storyboard Reference is used in the original Storyboard file to replace the removed View Controllers.




Reference video: What's new in Storyboards

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.