Understanding and use of delegate in IOS (value of the pass)

Source: Internet
Author: User

Before doing a half-year iOS, just entered the door, and was pulled to move to Java, now iOS that the big guy left, and was brought over to maintain the app, before the iOS delegate has been half-known, so just take advantage of this opportunity to write what I know, in order to consult later.

-Delegate's understanding

delegate, which is the proxy mode (a kind of design mode), I now understand the proxy mode is, when I have something to do but not free time, I can ask others to help, I tell you what I want to do, but specifically how to do, it is your business, for example, I want to go to meet a sister paper out to play, But I happen to have something to do now, or oneself embarrassed about, this time can ask a friend to help about, then Friend is your agent (commissioned) , you are the delegate, you tell him your purpose is to offer sister paper out to play, about sister paper out to play is you and his agreement between , as for how he put the sister paper out, you do not care.

Delegate the most common place is the inverse of the value (the forward value is directly passed the value of the property on the line)

What the general delegate needs to do:

1. Create the Protocol (that is, the method that the agent implements)

2. Declaring a delegate variable

3, set up the agent (can also be set in the agent)

4. Use the delegate variable to invoke the Protocol method (that is, let the agent start executing the protocol)

What the agent needs to do:

1. Follow the agreement

2. Implement the Protocol method

There are two pages of VCA, VCB, I first from VCA push to VCB, in a textfield of VCB input I want to pass the value, and so on click button Pop back to VCA, the VCB of the value in the view to display in the VCA label, At this time VCB is the delegate, VCA is the agent, the code is as follows:

1. Create an agreement

// Create an agreement in VCB // ViewControllerB.h // Create an agreement @protocol Vcbdelegate <nsobject>-(void// Declaration protocol method @end

2. Declaring a delegate variable

// ViewControllerB.h @interface Viewcontrollerb:uiviewcontroller@property (nonatomic, weak) ID Delegate // declaring protocol variables @end

3, set the agent (I was in the VCA button jump when the agent set, so is set in VCA.M)

// viewcontrollera.m-(void) btnclicked{    new];    VcB. delegate = self ;    [Self.navigationcontroller PUSHVIEWCONTROLLER:VCB animated:yes];}

4. Use the delegate variable to invoke the Protocol method (that is, let the agent start executing the protocol)

-(void) backactionclicked{    // Contemporary Response Sendvalue method, the value in _tx.text is passed to VCA    if ([_delegate respondstoselector: @selector (sendvalue:)]) {        [_delegate sendvalue:_tx.text];        [Self.navigationcontroller Popviewcontrolleranimated:yes];}    }

5. Following the agreement in the agent (Viewcontrollera)

// VIEWCONTROLLERA.M @interface Viewcontrollera () <VcBDelegate>// compliance with agreement  *recievedlb; @end

6, Agent implementation protocol method

// VIEWCONTROLLERA.M // implements the Protocol method to present the received value to the label -(void) Sendvalue: (NSString *) value{    = value;}

This will be OK.

Attached to the simulator:

Finally attach the demo git address: Https://github.com/fenglee594/delegateDemo

Do not like to spray, welcome to discuss.

Understanding and use of delegate in IOS (value of the pass)

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.