Reverse value transfer by iOS proxy and reverse value transfer by ios proxy

Source: Internet
Author: User

Reverse value transfer by iOS proxy and reverse value transfer by ios proxy

 

In the previous blog's iOS proxy protocol, I focused on parsing the concept of the delegated proxy protocol. This article will focus on their applications in development.

Suppose we have A requirement as follows: there is A button and A label on interface. Jump from interface A to interface B, enter A string in the input box of interface B, and display it on the label of interface. This is a typical example of reverse value transfer. The core of this example is: "enter A string in the input box of interface B and display it on the label of interface ". That is to say, "interface B entrusts interface A to display strings, page A is the proxy of interface B ". The delegate reversely transmits the value to the agent.

So how can we use the proxy design mode to achieve this demand?

In the program:

1. The tasks to be commissioned include:

1.1 define protocols and Methods

1.2 declare delegate Variables

1.3 set proxy

1.4 call the delegate method using the delegate variable

2. the proxy needs to do the following:

2.1 follow the agreement

2.2 Delegate Method

 

In BViewController. h:

// Define protocol and method
@protocol DeliverDetegate <NSObject>

-(void) setValue: (NSString *) string;

@end

@interface BViewController: UIViewController

// Declare the delegate variable
@property (weak, nonatomic) id <DeliverDetegate> B_Delegate;

@end
 

In BViewController.m:

@interface BViewController () <UITextFieldDelegate>

@property (strong, nonatomic) IBOutlet UITextField * DeliverText;

@end
 

-(IBAction) DeliverAction: (id) sender {
    
    // Call delegate method via delegate variable
    // Enter to display the input string, if not input, display "not filled in"
    if (! [_ DeliverText.text isEqualToString: @ ""]) {
        NSLog (@ "B sends data to A% @", _ DeliverText.text);
        // Determine whether the method in the agent is implemented to avoid the crash of the program that is not implemented in the agent
        if ([self.B_Delegate respondsToSelector: @selector (setValue :)])
        {
            [self.B_Delegate setValue: _DeliverText.text];
        }
    }
    else
    {
        NSLog (@ "B sends data to A% @", @ "not filled in");
        // Determine whether the method in the agent is implemented to avoid the crash of the program that is not implemented in the agent
        if ([self.B_Delegate respondsToSelector: @selector (setValue :)])
        {
            [self.B_Delegate setValue: @ "Not filled"];
        }
    }
    
    [self.navigationController popViewControllerAnimated: YES];
}
 

In AViewController.m

#import "AViewController.h"
#import "BViewController.h"

@interface AViewController () <DeliverDetegate>

@property (strong, nonatomic) IBOutlet UILabel * TextLabel;

@end
 

-(IBAction) ReceiveAction: (id) sender {
    
    // Follow the agreement
    BViewController * BVC = [[BViewController alloc] init];
    BVC.B_Delegate = self;
    [self.navigationController pushViewController: BVC animated: YES];
}
 

// Implement the delegate method, namely the setValue method
-(void) setValue: (NSString *) string
{
    NSLog (@ "A received B data% @", string);
    _TextLabel.text = string;
}
The final one is as follows:

 

Output log:

Because this article uses XIB, some UI details are omitted, and the code link of this article is attached: source code.

Where the writing is not good, I hope the comments will give pointers. Thanks ~

Related Article

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.