iOS development-use storyboard for interface jump and value transfer

Source: Internet
Author: User
Tags uikit

Apple officially recommends that we use storyboard to build all the UI, and storyboard is a very mature tool. Using storyboard to build all the interface, we can quickly build a complex interface, which means that we can save a lot of time. We can also be very intuitive to see the relationship between the various interfaces, modification is also very convenient. If we meet the need to make changes in the future, we just need to find the corresponding storyboard can be, compared to the previous, a lot faster.

I will attach the demo at the end of this article, can help you to have a more intuitive understanding, the need for people can go to download and run a bit.

In addition, we recommend that you experiment with each method by emptying the contents of the interface to avoid errors. Because beginners can easily make the interface residue, the specific here does not explain. Next, let's start our storyboard tour!

Directory:

First, use storyboard to jump

1) Pure Storybard interface operation

2) use code to jump

Second, storyboard interface transfer value

1) Use the Prepareforsegue method to jump:

2) Storyboard ID jump

First, use storyboard to jump

1) Pure Storyboard interface operation

We put a button on the storyboard, click on the top of the button and hold the "right" button, and drag to the next interface of the jump.

After releasing the right button, there will be a black transparent pop-up box, select the way to jump:

From here you can see that there is a line connection between the interface and the interface, such as:

We can see from the toolbar on the right of Xcode that we have just connected (now we can run the project):

2) use code to jump

We have just removed the connection, as shown in:

Create a new class Codeviewcontroller that inherits from Uiviewcontroller:

Select the interface where the button is located, set its class file, which is inherited from Uiviewcontroller by default, as shown in:

We changed it to the newly created class "Codeviewcontroller" as shown in:

To reduce the interface (you can double-click in a blank space, or right click on the scale), this time we do not directly use the "button" interface, but the interface and interface between the connection, as shown in:

Note: 100% of the zoom is not able to interface and interface between the connection!

After the operation and the previous consistency, in order to understand easily, I still paste one:

Select "This line" to specify an identifier in the storyboard Segue identifier, which we will use later:

At this point we need to add an event for the button to display both the storyboard and. m files, as follows:

The action to create the event is the same as the previous line:

Create a name for this event, then click Connect:

In the event, add the following code, the identifier of the just connected, sender is generally "self":

[Self performseguewithidentifier:@ "Easycode" sender:self];

In this way, you can successfully jump.

Second, storyboard interface transfer value

1) Use the Prepareforsegue method to jump:

We create two classes, Prepareviewcontroller and Receiveviewcontroller

Then create a new two viewcontroller on the storyboard and connect the two interfaces together. The procedure is the same as above (code jump), this time we define the identifier as "Sendvalue":

The class with a button-down interface is set to Prepareviewcontroller, as shown in:

Select the second interface, set the inherited class to Receiveviewcontroller:

ReceiveViewController.h:

#import <UIKit/UIKit.h> @interface Receiveviewcontroller:uiviewcontroller@property (Strong, Nonatomic) NSString *name; @property (assign, nonatomic) int age; @end

RECEIVEVIEWCONTROLLER.M, of course, is to output a bit, otherwise how to know whether the value of success:

#import "ReceiveViewController.h" @interface Receiveviewcontroller () @end @implementation receiveviewcontroller-( void) Viewdidload {    [super viewdidload];        NSLog (@ "name is%@, age is%d", _name, _age);} @end

PREPAREVIEWCONTROLLER.M file:

#import "PrepareViewController.h" #import "ReceiveViewController.h" @interface Prepareviewcontroller () @ End@implementation prepareviewcontroller-(ibaction) Goaction: (ID) sender{    //jump to target VC according to the specified line ID    [self performseguewithidentifier:@ "Sendvalue" sender:self];} -(void) Prepareforsegue: (Uistoryboardsegue *) Segue Sender: (ID) Sender {    //Segue.identifier: Gets the ID of the connection    if ([ Segue.identifier isequaltostring:@ "Sendvalue"]) {        //Segue.destinationviewcontroller: Access to the Interface (VC)        referred to in the connection Receiveviewcontroller *receive = Segue.destinationviewcontroller;        Receive.name = @ "Garvey";        Receive.age =;        There is no need to specify a jump because there is already a jump code in the event of a twist//        [Self.navigationcontroller pushviewcontroller:receive animated:yes];    } @end

The value can be successfully received (done):

2) Storyboard ID jump

Continue to drag two pure interface (VC) to storyboard, the first interface is also put on a UIButton:

Idviewcontroller is my newly-created class, the interface that receives the value is used back to the Receiveviewcontroller class, the specified interface inherits the class as (Idviewcontroller, Receiveviewcontroller)

The second interface is set to Storyboard id "idreceive", followed by a transfer to:

Idviewcontroller.m

-(Ibaction) Action: (ID) sender{    //Gets the specified Storyboard,name fill in the storyboard filename    uistoryboard *storyboard = [ Uistoryboard storyboardwithname:@ "Main" bundle:nil];    To get the specified interface (VC) from storyboard on identifier, identifier must be unique    receiveviewcontroller *receive = [Storyboard instantiateviewcontrollerwithidentifier:@ "Idreceive"];    Receive.name = @ "GC";    Receive.age = ten;    [Self.navigationcontroller pushviewcontroller:receive animated:yes];}

Can run the program, can successfully receive the value, success!

Summary: Can you see the advantages and disadvantages of each method? It doesn't matter now, when you meet the real need, the pros and cons are easy to know, which is left to you to think of something.

Off Topic:

Some people do not agree with the use of storyboard, their views are as follows:

1) Storyboard errors can not be debugged, often making them slow to develop

2) for multi-person development, version management will be more complex

3) Storyboard often trouble for no apparent reason! Make them have a headache!

First, for the 1th and 3rd answers: Storyboard errors can often be avoided. Second, storyboard will not cause problems for no reason, if there is a problem, it is often the developer's improper operation. Why would I say that?

Because I have met a lot of errors caused by storyboard, then I also often blame storyboard. But as I became more and more familiar with storyboard, I found that it was all because of improper operation and caused the collapse of the program.

2nd: Multi-person development, can distinguish between module development, everyone responsible for the module is not the same, not necessarily all of us have to develop the same module. Of course, there are special circumstances I do not know, so that this needs to be based on the size and complexity of the project to determine.

Spent an afternoon to write this article, I feel good tired tired.

I hope this article can be of help to everyone, thank you.

Demo:

Http://pan.baidu.com/s/1bnCkydD

Blog Garveycalvin

Blog Source: http://www.cnblogs.com/GarveyCalvin/

iOS development-use storyboard for interface jump and value transfer

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.