[IOS7 Summary] 3. data transmission between ViewControllers of the View Controller (3)

Source: Internet
Author: User

This document records the data transfer mechanism between different view controllers when UI is implemented by handwriting code During interface switching. The presentViewController and dismissViewController functions are used to display and remove functions on the interface. The proxy and notification mechanisms can be used for data transmission. This article records the use of storyboard to implement the interface, and use the method related to segue to implement the same functions as above.

The first thing to do is to create a single view project. After the project is created, a group of AppDelegate classes (a header file and a source file are called a group) and a group of ViewController classes are generated, a storyboard and other auxiliary files. Create a New View Controller class, use the shortcut key command + N, select the Objective-C class, and create a set of view controllers named FirstSubViewController inherited from UIViewController.

Add various controls in the storyboard. According to the previous design, add a Label and a Button to the first sence. Then, drag a New View Controller Into the storyboard and add a TextField and a Button to it. Do not forget to add IBOutlet and IBAction to the control, and change the ViewController class of the second interface to the FirstSubViewController class we created previously.

It is very easy to create a segue between different interfaces. You only need to press Ctrl on the trigger control (such as Button) and drag it to the new interface, and select the Modal option in the pop-up menu. Add an ID for the segue in the Attribute menu, and then use a similar method to send the second ViewController button to the first interface.

 

Then, a segue between the two interfaces is created, as shown in.

 

After running the program, you will find that clicking "show next interface button" on the first interface will jump to the second interface. Click "back to previous interface ", it will jump back to the original interface.

Then let the Label in the first interface obtain the content in TextField In the second interface. The implementation method is very simple, that is, to implement the prepareForSegue function in the View Controller of the second interface.

Add a property in ViewController. h to receive the passed characters:

 

@property (nonatomic,copy) NSString *stringToReceive;

 

Note that you should not try to declare the IBOutlet of the UILabel type as a public attribute for receiving strings, because in the following prepareForSegue method, you cannot directly obtain a valid Label instance.

Then, in the viewDidLoad function, determine whether the attribute is null. If it is not null, the attribute is assigned to the tag:

 

- (void)viewDidLoad{    [super viewDidLoad];// Do any additional setup after loading the view, typically from a nib.    if (_stringToReceive != Nil)    {        _showText.text = _stringToReceive;    }}

 

Implement functions in firstSubViewController:

 

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{    if ([segue.identifier isEqualToString:@"goBackToPreviousViewController"])    {        if ([segue.destinationViewController isKindOfClass:[ViewController class]])        {            ViewController *vc = (ViewController *)segue.destinationViewController;            vc.stringToReceive = _inputText.text;        }    }}
Here, "gobacktopreviusviewcontroller" is the custom segue ID.

 

Since then, the entire project has been completed. After running the test, everything is normal.

This article and the above two demos have been uploaded to the resource page, you can download as a reference.

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.