Use delegate to pass values between two Viewcontroller

Source: Internet
Author: User


The following implementation of the use of delegate in two viewcontroller between the value, this scenario is generally applied to enter the sub-interface input information, after the completion of the input information back to the previous interface , such as modify the user's personal information, click Modify to enter the modified interface, After the modification, the display screen displays the modified results.

The method declared in the agreement:

    1. #import <Foundation/Foundation.h>
    2. @class userentity;
    3. @protocol passvaluedelegate <NSObject>
    4. -(void) Passvalue: (userentity *) value;
    5. @end


Implement the Protocol in the first window:

    1. #import <UIKit/UIKit.h>
    2. #import "PassValueDelegate.h"
    3. The first window complies with Passvaluedelegate
    4. @interface viewcontroller:uiviewcontroller<passvaluedelegate>
    5. @property (Retain, nonatomic) Iboutlet UILabel *namelabel;
    6. @property (Retain, nonatomic) Iboutlet UILabel *agelabel;
    7. @property (Retain, nonatomic) Iboutlet UILabel *gendarlabel;
    8. -(Ibaction) openbtnclicked: (ID) sender;
    9. @end


Ways to implement protocols in. m Files:

[CPP]View Plaincopy < param name= "wmode" value= "Transparent" >
    1. Implement the Protocol in the first window to display the value entered in the second window, similar to the Onactivityresult method in Android
    2. -(void) Passvalue: (userentity *) value
    3. {
    4. Self.nameLabel.text = Value.username;
    5. Self.ageLabel.text = [NSString stringwithformat:@ "%d", value.age];
    6. Self.gendarLabel.text = Value.gendar;
    7. }


Click on the event triggered by the Open button:

//Click on the method to enter the second window

    1. -(Ibaction) openbtnclicked: (ID) Sender {
    2. Secondviewcontroller *secondview = [[Secondviewcontroller alloc] initwithnibname:@ "Secondviewcontroller" bundle:[ NSBundle Mainbundle]];
    3. Set the delegate in the second window to the self of the first window
    4. Secondview.delegate = self;
    5. [Self.navigationcontroller Pushviewcontroller:secondview Animated:yes];
    6. [Secondview release];
    7. }


The second window declares a NSObject object that adheres to the Passvaluedelegate protocol:


    1. #import  <uikit/uikit.h>  
    2. #import   "PassValueDelegate.h"   
    3.   
    4. @interface  secondviewcontroller : uiviewcontroller   
    5.   
    6. @property   (retain, nonatomic)  IBOutlet  uitextfield *nametextfield;  
    7. @property   (retain, nonatomic)  iboutlet  uitextfield *agetextfiled;  
    8. @property   (retain, nonatomic)   iboutlet uitextfield *gendartextfield;  
    9.   
    10. // This is done with assign instead of retain to prevent circular references from being generated.   
    11. @property (nonatomic,assign)  NSObject<PassValueDelegate> *delegate;   
    12.   
    13. -  (ibaction) okbtnclicked: (ID) sender;  
    14. -  ( ibaction) Closekeyboard: (ID) sender;  
    15.   
    16. @end   


When you are finished typing, click the OK button to trigger the event:

  1. -(Ibaction) okbtnclicked: (ID) Sender {
  2. userentity *userentity = [[Userentity alloc] init];
  3. Userentity.username = Self.nameTextField.text;
  4. Userentity.gendar = Self.gendarTextField.text;
  5. Userentity.age = [Self.ageTextFiled.text intvalue];
  6. Passing a value through a delegated protocol
  7. [Self.delegate passvalue:userentity];
  8. Go back to the first window
  9. [Self.navigationcontroller Popviewcontrolleranimated:yes];
  10. [Userentity release];
  11. }

Use delegate to pass values between two Viewcontroller

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.