Lvesli Original, reproduced please specify the original link thank you http://www.androiddev.net/lvesli_delegate/
A delegate is a behavior that gives an object an opportunity to react to a change in another object, or a corresponding other object. The basic idea is to solve the problem together.
In the program: in general
1. The tasks to be commissioned are:
1.1 Defining protocols and methods
1.2 Declaring a delegate variable
1.3 Setting up Proxies
1.4 Calling a delegate method from a delegate variable
2. The work that the agent needs to do is:
2.1 Following the agreement
2.2 Implementing a Delegate method
Here's a workaround for using delegates to implement page-pass values:
Join us There are two Viewcontroller, respectively: Viewcontroller and Trendviewcontroller
A. Requirements:
We want to implement a feature that jumps to Trendviewcontroller immediately when you click on a button on the Viewcontroller view and passes a string of strings past.
Two. Solution:
1. The most up-to-the-top declaration agreement in the ViewController.h file is as follows:
1 |
@protocol PassTrendValueDelegate |
2 |
-( void )passTrendValues:( NSString *)values; //1.1定义协议与方法 |
2. Continue declaring a delegate variable in ViewController.h
2 |
@property (retain, nonatomic ) id <PassTrendValueDelegate> trendDelegate; |
3. Enter the VIEWCONTROLLER.M file in the Click Button Event function, set the proxy
4 |
TrendViewController *trendViewController = [[TrendViewController alloc] initWithNibName: @"TrendViewController" bundle: nil ]; |
6 |
self .trendDelegate=trendViewController; //设置代理 |
7 |
[ self .trendDelegate passTrendValues: @"Lves李兴乐" ]; |
4. Enter TrendViewController.h, refer to Viewcontroller's header file, and add the proxy protocol as follows:
1 |
#import "ViewController.h" |
3 |
@interface TrendViewController : UIViewController<PassTrendValueDelegate>{ |
5. Implement the Proxy function:
2 |
-( void )passTrendValues:( NSString *)values{ |
3 |
NSLog ( @"values:::%@" ,values); |
Run OK.
There are also ways to implement page pass values:
1. Notice
2. Methods
3. Proxy methods
4.SharedApplication
5.NSUserdefault
6. Pass through a single instance of class
IOS Delegate Agent (delegate) implements page pass value