Storyboard Learning (5): transfer data between pages using segue

Source: Internet
Author: User
Tags close page

Storyboard Learning (5): transfer data between pages using segue

 

 

Function:

 

C code
  1. -(Void) prepareforsegue :( uistoryboardsegue *) segue sender :( ID) sender

 

Example:

 

1. First create a single view template project, and then add a new viewcontronler in mainstoryboard. Add tags, buttons, and edit input boxes to the two view controllers.

2. Create a segue for pages 1st and 2nd.

Select the button on the 1st page [jump to page 2nd], right-click the page (or press the controll key, left click), drag it to page 2nd, and select [modal] from the pop-up menu.

 

3. hook the edit input box component on page 1st with the class file.

Open the mainstoryboard and viewcontroller. h file in a separate window, right-click the file, and drag and drop the text box to edit the next line of @ interface in the. h file.

Note: edit the name attribute of the component in the input box and set it to page1data.

 

4. Add the following code to viewcontroller. M.

 

After @ implementation, add:

 

 

C code
  1. @ Synthesize page1data;

 

The method for reloading prepareforsegue is as follows:

 

C code
  1. -(Void) prepareforsegue :( uistoryboardsegue *) segue sender :( ID) sender {
  2. Nsstring * Data = page1data. text;
  3. Uiviewcontroller * view = segue. destinationviewcontroller;
  4. If ([view respondstoselector: @ selector (setparam :)]) {
  5. [View setvalue: Data forkey: @ "Param"];
  6. }
  7. }

 

Note: setparam And Param are associated with the following code !!

 

 

5. Add a new class file and bind it to page 2nd.


Note: The class name is secondviewcontroller.


Bind the class secondviewcontroller to page 2nd.

 

6. Bind the edit input box component on the 2nd page to the secondviewcontroller class.

In a separate window, open secondviewcontroller. h and mainstoryboard, select the edit input box on the 2nd page, right-click and drag the edit input box to the next line of @ interface in the secondviewcontroller. h file.

 

Note: When binding, edit the name attribute of the input box component and set it to page2data.

 

 

7. Modify the. h and. m Files of secondviewcontroller.

7.1 modify secondviewcontroller. h

@ Interface secondviewcontroller: After uiviewcontroller, add:

 

C code
  1. @ Property (strong, nonatomic) nsstring * Param;

Note: The param parameter defined here must be the same as the param in the prepareforsegue method in the previous viewcontroller. M !!!

 

 

7.2 modify secondviewcontroller. m

Add the following after @ implementation secondviewcontroller:

 

C code
  1. @ Synthesize Param;
  2. @ Synthesize page2data;

Modification Method viewdidload:

 

C code
  1. -(Void) viewdidload
  2. {
  3. [Super viewdidload];
  4. // Do any additional setup after loading the view.
  5. Page2data. Text = Param;
  6. }

 

 

Compile and run the program. When you enter parameters on page 1st, the system will display the parameters entered on page 2nd.

 


 

 

8. Add an event to the button [Close Window] on page 1.

Open secondviewcontroller. h and mainstoryboard in a separate window, select the button [Close Window] on page 1, right-click and drag it to secondviewcontroller. H, and right-click and drag it to @ interface secondviewcontroller: uiviewcontroller;

 

In the pop-up window, select action for the connection attribute and set the name attribute to closewin. Click [connect].

 

 

The following code is added to secondviewcontroller. h:

 

C code
  1. -(Ibaction) closewin :( ID) sender;

 

The following code is added to secondviewcontroller. M:

 

C code
  1. -(Ibaction) closewin :( ID) sender {
  2. }

 

Modify the closewin method in secondviewcontroller. m. The Code is as follows:

 

 

C code
  1. -(Ibaction) closewin :( ID) sender {
  2. [Self dismissviewcontrolleranimated: Yes completion: Nil];
  3. }

 

 

Compile and run the program. After page 1 is displayed, click the [Close Window] button to close page 1 and display page 2 again.

 

Next, modify the received parameters on page 1, and then return the modified parameters to page 3.

 

 

9. modify the content of viewcontroller. h and. M files.

 

9.1 viewcontroller. h

After @ interface viewcontroller: uiviewcontroller, add:

 

C code
  1. @ Property (strong, nonatomic) nsstring * editdata;

 

Note: The Defined variable editdata is used to receive the content of the 2nd page edit input box, which will be used in secondviewcontroller. M !!!

 

 

9.2 viewcontroller. m

Add the following after @ implementation viewcontroller:

 

C code
  1. @ Synthesize editdata;

 

Method of modification-(void) prepareforsegue :( uistoryboardsegue *) segue sender :( ID) sender

 

C code
  1. -(Void) prepareforsegue :( uistoryboardsegue *) segue sender :( ID) sender {
  2. Nsstring * Data = page1data. text;
  3. Uiviewcontroller * view = segue. destinationviewcontroller;
  4. If ([view respondstoselector: @ selector (setparam :)]) {
  5. [View setvalue: Data forkey: @ "Param"];
  6. }
  7. If ([view respondstoselector: @ selector (setfirstviewcontroller :)]) {
  8. [View setvalue: Self forkey: @ "firstviewcontroller"];
  9. }
  10. }

 

Note: The setfirstviewcontroller and firstviewcontroller are defined in secondviewcontroller. h and. m below !!!

 

 

 

Overload method-(void) viewwillappear :( bool) animated:

 

C code
  1. -(Void) viewwillappear :( bool) animated {
  2. Nslog (@ "viewwillappear ");
  3. [Super viewwillappear: animated];
  4. Page1data. Text = editdata;
  5. }

 

10. modify the content of the secondviewcontroller. h and. M files.

 

10.1 secondviewcontroller. h

Add the following after @ interface secondviewcontroller: uiviewcontroller:

 

C code
  1. @ Property (strong, nonatomic) ID firstviewcontroller;

 

Note: The variable name firstviewcontroller must be the same as that in viewcontroller. M !!!

 

10.2 secondviewcontroller. m

Add the following after @ implementation secondviewcontroller:

 

C code
  1. @ Synthesize firstviewcontroller;

 

Overload method-(void) viewwilldisappear :( bool) animated

 

C code
  1. -(Void) viewwilldisappear :( bool) animated {
  2. [Super viewwilldisappear: animated];
  3. If ([firstviewcontroller respondstoselector: @ selector (seteditdata :)]) {
  4. [Page2data endediting: Yes];
  5. [Firstviewcontroller setvalue: page2data. Text forkey: @ "editdata"];
  6. }
  7. }

Note: The seteditdata and editdata must be the same as those defined in viewcontroller. h and. M !!!

 

 

 

OK, compile and run. The input text on the 1st page is passed to the input box on the 2nd page. The text in the input box on the 2nd page is also passed to the input box on the 1st page after the window is closed.

Original article: http://stephen830.iteye.com/blog/1696614

Storyboard Learning (5): transfer data between pages using segue

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.