IOS Proxy reverse-pass value

Source: Internet
Author: User

In the previous blog iOS Agent protocol, focusing on the concept of principal-agent protocol, this article will focus on their application in the development.

If we have a demand as follows: interface A has a button, a label. From interface a jump to interface B, enter the string in the input box of interface B and display it on the label of interface A. This is a typical example of a reverse value. The core of this example is: "Enter the string in the input box of interface B and display it on the label on interface A." That is: "Interface B Delegate interface a display string, page A is the agent of interface B." The delegate direction agent reverses the value.

So how do we use the proxy design pattern to implement this requirement?

In the program:

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

In the BViewController.h:

Define protocols and methods @protocol deliverdetegate <nsobject>-(void) SetValue: (NSString *) string; @end @interface bviewcontroller:uiviewcontroller//declares the delegate variable @property (weak,nonatomic) id<deliverdetegate>b_delegate; @end

In the BVIEWCONTROLLER.M:

@interface Bviewcontroller () <UITextFieldDelegate> @property (Strong, nonatomic) iboutlet Uitextfield * Delivertext; @end

-(Ibaction) Deliveraction: (ID) Sender {/        /The delegate method//input through the delegate variable    displays the input string, not entered displays "not filled"    if (![ _delivertext.text isequaltostring:@ ""]) {        NSLog (@ "b to send data%@", _delivertext.text);        Determine if the method in the proxy is implemented, and avoid a program that is not implemented by the agent crashes        if (self. B_delegate respondstoselector: @selector (setValue:)])        {            [self. B_delegate Setvalue:_delivertext.text];        }    }    else    {        NSLog (@ "B sends data%@", @ "not filled in");        Determine if the method in the proxy is implemented, and avoid a program that is not implemented by the agent crashes        if (self. B_delegate respondstoselector: @selector (setValue:)])        {            [self. B_delegate setvalue:@ "not filled"];        }    }        [Self.navigationcontroller Popviewcontrolleranimated:yes];}

In the AVIEWCONTROLLER.M

#import "AViewController.h" #import "BViewController.h" @interface Aviewcontroller () <deliverdetegate>@ Property (Strong, Nonatomic) Iboutlet UILabel *textlabel; @end

-(Ibaction) Receiveaction: (ID) Sender {        //follow protocol    BVIEWCONTROLLER*BVC = [[Bviewcontroller alloc]init];    BVC. B_delegate = self;    [Self.navigationcontroller PUSHVIEWCONTROLLER:BVC animated:yes];}

Implementation of the delegate method, that is, the implementation of the SetValue Method-(void) SetValue: (NSString *) string{    NSLog (@ "A received B data%@", string);    _textlabel.text = string;}

The last of the following:

Output log:

As this article is using Xib, omitted some UI details, attached to the code of this article link: source.

Where to write bad, hope to comment on the guidance. thanked ~

IOS Proxy reverse-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.