Storyboard and custom Toggle effects

Source: Internet
Author: User

1. The storyboard is not perfect, it consists of some obvious deficiencies. But storyboards are a trend in building user interfaces and how to use storyboards to accomplish things that are done with a nib file (such as communicating with a controller).

2. Add a Storyboard: The same way you add a new file. In fact, starting with Xcode 5, the use storyboard option has been turned on by default, making it more difficult to remove storyboards from the app.

Properties in 3.info.plist: When you create a new project using the storyboard, the Info.plist key that is applied contains a key named Uimainstoryboardfile. This key replaces the nsmainnibfile previously used by IOS5. If the main window of your app is loaded from a nib file instead of from a storyboard file, you can continue to use Nsmainnibfile. However, you cannot use both Uimainstoryboardfile and nsmainnibfile in the same application. Uimainstoryboardfile will preempt, and the nib file specified in Nsmainnibfile will never be loaded.

4. Instantiating storyboards

+ (Uistoryboard *) Storyboardwithname: (NSString *) name bundle: (nsbundle*) Storyboardbundleornil;

5. Loading the storyboard is similar to trying to load the controller and loading the nib file. With the Uistoryboard object, you can instantiate the controller in the following ways:

-(ID) Instantiateinitialviewcontroller;

-(ID) Instantiateviewcontrollerwithidentifier: (NSString *) identifier;

6. Wire (Segue)

A line (segue) is a toggle effect defined in a storyboard file. The Uikit provides two default switching effects: Push and modal. Their behavior is similar to the pushViewController:animated:completion used in iOS 5: and the PresentViewController:animated:completion method. In addition, we can create custom lines to create transitions between new view controllers.

In the storyboard file, create a line by connecting specific events on the view controller to other view controllers. You can drag and drop from the button onto the view controller, drag and drop from the gesture Recognizer object onto the view controller, and so on. IB creates a line between them, and we can select that line and use the feature Viewer panel to modify its toggle style.

If you choose a custom switch style, the feature Viewer panel also allows us to set custom classes. You can use the line as the connection action and the toggle effect thing. The action that triggers the link can be a button click event, a row selection event on a static table view, a recognized gesture, or even an audio event. The compiler automatically generates the necessary code to execute the line when an event that is bound to a line occurs.

The line execution invokes the Prepareforsegue:sender: method in the Source view controller and passes an object of type Uistoryboardsegue to the method. This method can be overridden to pass data to the target view controller.

When a view executes multiple lines, each line calls the same prepareforsegue:sender: method. To identify the lines that have been executed, you need to use a line identifier to check if the executed line is intended to be executed and pass the data accordingly. In accordance with defensive programming practices, it is recommended that you always perform this check, even if the view controller executes only one line. This guarantees that the app will continue to run without crashing when new lines are added later.

6.1. Passing Data

6.2. Return Data

6.3. Instantiating other view controllers

6.4. Manual Wiring

Although storyboards can automatically trigger lines based on actions, in some cases it is still necessary to implement the wiring by programming. We may need to do this to handle actions that cannot be handled by the storyboard file. To wire, you need to call the view controller's Performseguewithidentifier:sender: method. You can pass the caller and the context object in the sender parameter when you manually wire the line. Later, this sender parameter is sent to the Prepareforsegue:sender: method.

6.5. Expand the line.

7. Using storyboards to implement a table view

One important advantage of using storyboards is that you can create static tables directly from IB. Storyboards allow you to build two types of table views: a static table that does not require a special class to provide a data source, and a table that contains the prototype cells that bind the data in the model.

7.1. Static table

The static cell is used to create the settings interface, such as the Apple Setup interface. Note: You can only create a static cell for the table view generated by Uitableviewcontroller, and the table view that you added as a child view of the Uiviewcontroller view cannot be created.

7.2. Prototype cell (Prototype Cells)

Note: If the prototype cell in the storyboard does not have a cell identifier, Xcode warns you.

8. Custom transition effects

Another advantage of the storyboard is that it allows us to easily create custom transitions for the view controller.

When wiring is in progress, the compiler generates relevant code to notify or push to the target view controller based on the switching style set by the developer in the storyboard.

Custom class: Customsegue, which inherits from Uistoryboardsegue.

Custom Transition Example: Set the segue in the storyboard to custom, marking its class as Customsegue.

To override the Perform method in CUSTOMSEGUE.M:

As follows:

9. Pros and Cons

Pros: Collaborative developers (and customers) can easily understand the workflow of the app, and open the storyboard to see the entire workflow.

Cons: Merge conflicts When multiple developers are collaborating.

Storyboard and custom Toggle effects

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.