iOS design mode-delegate mode

Source: Internet
Author: User

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:

@protocol passtrendvaluedelegate-(void) Passtrendvalues: (NSString *) values; // 1.1 Defining protocols and methods @end

2. Continue declaring a delegate variable in ViewController.h

/// 1. Define a delegate variable to pass a value to a trend page ID <PassTrendValueDelegate> trenddelegate;

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

#pragma Mark click the Trend button-(void)    trendbtnclick{//Create the View    Trendviewcontroller *trendviewcontroller = [[Trendviewcontroller alloc] Initwithnibname:@ "  Trendviewcontroller"Bundle:nil"  ;    Self.trenddelegate// set Agent    [self.trenddelegate passtrendvalues:@ "ABC " ]; // page jump Save }

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

#import " ViewController.h " @interface Trendviewcontroller:uiviewcontroller<passtrendvaluedelegate>{}@end

5. Implement the Proxy function :

#pragma<span id= "0_nwp" style= "Width:auto; Height:auto; Float:none; " ><a id= "0_nwl" href= "http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv= 18&is_app=0&jk=3d222ce8b1c3dc1c&k=mark&k0=mark&kdi0=0&luki=3&n=10&p=baidu& q=69003180_cpr&rb=0&rs=1&seller_id=1&sid=1cdcc3b1e82c223d&ssp2=1&stid=0&t= tpclicked3_hc&td=2116751&tu=u2116751&u=http%3a%2f%2fwww%2eandroiddev%2enet%2flvesli%5fdelegate%2f &urlid=0 "target=" _blank "mpid=" 0 "style=" text-decoration:none; " ><span style= "color: #0000ff; font-size:12px;width:auto;height:auto;float:none;" >mark</span></a></span> implementing the method of value-Transfer Protocol-(void) Passtrendvalues: (NSString *) values{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 design mode-delegate mode

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.