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:
- #import <Foundation/Foundation.h>
- @class userentity;
- @protocol passvaluedelegate <NSObject>
- -(void) Passvalue: (userentity *) value;
- @end
Implement the Protocol in the first window:
- #import <UIKit/UIKit.h>
- #import "PassValueDelegate.h"
- The first window complies with Passvaluedelegate
- @interface viewcontroller:uiviewcontroller<passvaluedelegate>
- @property (Retain, nonatomic) Iboutlet UILabel *namelabel;
- @property (Retain, nonatomic) Iboutlet UILabel *agelabel;
- @property (Retain, nonatomic) Iboutlet UILabel *gendarlabel;
- -(Ibaction) openbtnclicked: (ID) sender;
- @end
Ways to implement protocols in. m Files:
[CPP]View Plaincopy < param name= "wmode" value= "Transparent" >
- Implement the Protocol in the first window to display the value entered in the second window, similar to the Onactivityresult method in Android
- -(void) Passvalue: (userentity *) value
- {
- Self.nameLabel.text = Value.username;
- Self.ageLabel.text = [NSString stringwithformat:@ "%d", value.age];
- Self.gendarLabel.text = Value.gendar;
- }
Click on the event triggered by the Open button:
//Click on the method to enter the second window
- -(Ibaction) openbtnclicked: (ID) Sender {
- Secondviewcontroller *secondview = [[Secondviewcontroller alloc] initwithnibname:@ "Secondviewcontroller" bundle:[ NSBundle Mainbundle]];
- Set the delegate in the second window to the self of the first window
- Secondview.delegate = self;
- [Self.navigationcontroller Pushviewcontroller:secondview Animated:yes];
- [Secondview release];
- }
The second window declares a NSObject object that adheres to the Passvaluedelegate protocol:
- #import <uikit/uikit.h>
- #import "PassValueDelegate.h"
-
- @interface secondviewcontroller : uiviewcontroller
-
- @property (retain, nonatomic) IBOutlet uitextfield *nametextfield;
- @property (retain, nonatomic) iboutlet uitextfield *agetextfiled;
- @property (retain, nonatomic) iboutlet uitextfield *gendartextfield;
-
- // This is done with assign instead of retain to prevent circular references from being generated.
- @property (nonatomic,assign) NSObject<PassValueDelegate> *delegate;
-
- - (ibaction) okbtnclicked: (ID) sender;
- - ( ibaction) Closekeyboard: (ID) sender;
-
- @end
When you are finished typing, click the OK button to trigger the event:
- -(Ibaction) okbtnclicked: (ID) Sender {
- userentity *userentity = [[Userentity alloc] init];
- Userentity.username = Self.nameTextField.text;
- Userentity.gendar = Self.gendarTextField.text;
- Userentity.age = [Self.ageTextFiled.text intvalue];
- Passing a value through a delegated protocol
- [Self.delegate passvalue:userentity];
- Go back to the first window
- [Self.navigationcontroller Popviewcontrolleranimated:yes];
- [Userentity release];
- }
Use delegate to pass values between two Viewcontroller