IOS Delegate Agent (delegate) implements page pass value

Source: Internet
Author: User

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 @protocolPassTrendValueDelegate
2 -(void)passTrendValues:(NSString*)values;//1.1定义协议与方法
3 @end

2. Continue declaring a delegate variable in ViewController.h

1 ///1.定义向趋势页面传值的委托变量
2 @property(retain,nonatomic) id<PassTrendValueDelegate> trendDelegate;

3. Enter the VIEWCONTROLLER.M file in the Click Button Event function, set the proxy

1 #pragma mark 点击趋势按钮
2 -(void)trendBtnClick{
3     //create the view
4     TrendViewController *trendViewController = [[TrendViewController alloc] initWithNibName:@"TrendViewController"bundle:nil];
5
6     self.trendDelegate=trendViewController; //设置代理
7     [self.trendDelegate passTrendValues:@"Lves李兴乐"];
8 //页面跳转 省
9 }

4. Enter TrendViewController.h, refer to Viewcontroller's header file, and add the proxy protocol as follows:

1 #import "ViewController.h"
2
3 @interfaceTrendViewController : UIViewController<PassTrendValueDelegate>{
4
5 }
6 @end

5. Implement the Proxy function:

1 #pragma mark 实现传值协议方法
2 -(void)passTrendValues:(NSString*)values{
3     NSLog(@"values:::%@",values);
4
5 }

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

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.