Objective
In the development of iOS, several kinds of reverse transfer value, there are agents (delegate), Notice (nsnotification), block and so on, this article gives us analysis, how to understand and quick to start agent mode, and with a simple reverse transfer value as a case, Look at agent mode is not very difficult.
A few concepts in the agent model
Before you talk about proxy mode, you need to understand two concepts: the Agent object and the proxy object. and need to know that they are connected by agreement.
1, the Agent object
The agent is often the object of the real intention to do things, such as selling houses in the case of the owners want to sell the house, nanny baby cases want to drink milk baby. But they can't do it by themselves, only with the help of real estate agents and nannies.
2, proxy object
The agent is the object of the Real act, as mentioned above to help sell the house of the intermediary and care for the baby nanny.
3. Agreement
Proxy objects must have a standard to become agents, such as only the certificate can become a real estate intermediary, the owner dared to give him the house agent, only the nanny to the domestic service dare to let her come to take care of the baby, it is impossible to let the nanny to sell the house, let intermediary care for the baby And this specification is an agreement, who is the agreement? The answer must be the proxy object.
Second, the agent mode of writing norms
After figuring out the basic concept, the next is the writing agent mode, many students do not know how to do it, in fact, this is also a trace to follow.
The object being represented
1, the definition of the Protocol, in the Protocol to define the method used to pass the value, what type of value to set what type of parameters
2. Declare a delegate attribute of complying with the above agreement
3, when the need to pass the value of the call Agent object to complete the transfer value
Proxy Object
1. Abide by the agreement defined by the proxy object
2, implement the method in the protocol, capture the passed value, and process the captured value according to the requirement
Set the delegate property of the proxy object as the proxy object
This step is done in one of the two objects
Third, the agent model and the reverse transmission value
As can be seen from the description, in fact, agent mode is just a way to solve a particular problem, it has its own use of the scene, such as a want to listen to some of the changes in B, a can not handle some things themselves. As the saying goes no Code, no BB, here is a real case: now there are two interfaces, as shown in Figure 1 and Figure 2, the interface is very simple, no longer repeat, mainly in the first interface click the Jump button to reach the second interface, and then the second interface in the input box to enter information and then click the Return button, The Uilabel in the first interface can display the input of the second interface, which is the simplest one for the reverse transfer value.
Iv. implementation Steps
Analysis: now is the second interface to pass the value, but they do not, so it is a proxy object, the first interface is naturally the proxy object. Then write the code according to the specification above.
1, the Agent object
The. h file//The agent objects do the following several things//1, create a protocol, for the transfer value//2, declare a compliance with the above agreement delegate attributes/** * Create a protocol, which has a method, with a parameter, which is the value I want to get out. * * @protocol PA
Ssvalue <NSObject>-(void) Passedvalue: (NSString *) Inputvalue; @end @interface Nextviewcontroller:uiviewcontroller/** * Declares a delegate attribute */@property (nonatomic, weak) Id<passvalu
E> delegate; @end =================================================================//.m file #import "NextViewController.h" @
Interface Nextviewcontroller () @property (weak, nonatomic) Iboutlet Uitextfield;
-(Ibaction) Back: (ID) sender;
@end @implementation Nextviewcontroller-(void) viewdidload {[Super viewdidload];
Self.navigationItem.title = @ "Second interface"; /** * Returns to the previous interface * * @param sender < #sender description#> * *-(Ibaction) Back: (ID) sender {nsstring *inputstrin
g = Self.inputText.text; 3. Call the proxy object to complete the pass value if (self.delegate && [self.delegate respondstoselector: @selector (passedvalue:)]) {[
Self.delegate passedvalue:inputstring]; } [Self.navigAtioncontroller Popviewcontrolleranimated:yes]; } @end
2, proxy object
. h file
#import <UIKit/UIKit.h>
#import "NextViewController.h"//
Proxy object
//implement the protocol created by the proxy object. To implement the method, capture the passed value
@interface viewcontroller:uiviewcontroller <passValue>
@end
================== ===============================================
//.m file
#import "ViewController.h"
@interface Viewcontroller ()
@property (weak, nonatomic) Iboutlet Uilabel *nextpassedvalue;
-(Ibaction) Next: (ID) sender;
@end
@implementation Viewcontroller
-(void) viewdidload {
[super Viewdidload];
}
/**
* Implements the method of the proxy object, sets the captured value to Uilabel
*
* @param the value of Inputvalue
(void) Passedvalue: ( NSString *) inputvalue{
self.nextPassedValue.text = inputvalue;
}
Click the return button to jump to the second interface
-(ibaction) Next: (ID) Sender {
Nextviewcontroller *NVC = [[Nextviewcontroller alloc]init];
//Set the delegate property of the proxy object to be the proxy object i.e. self
nvc.delegate = self;
[Self.navigationcontroller PUSHVIEWCONTROLLER:NVC animated:yes];
}
@end
Five, the realization effect
Summarize
The above is the entire content of this article, I hope the content of this article for your iOS developers can help, if you have questions you can message exchange.