A simple understanding of the iOS proxy mode: When an object cannot get a pointer to another object directly, and you want to do something about that variable, you can use proxy mode.
The agent mainly consists of three parts:
(1) Protocol: Used to specify what the agent can do and what must be done.
(2) Agent: According to the specified protocol, complete the functions that the client needs to implement.
(3) Delegate: According to the specified protocol, specify the agent to complete what function.
Agent use Step 1. Declare a protocol (this is written in the object that needs to be obtained, you can also write a separate class)
@protocol textdelegate
-(void) Deliverfirsttext: (nsstring*) Text1 withdeliversecondtext: (NSString *) Text2;
@end
2. Agent#import #import "LoginProtocol.h"@interface LoginViewController : UIViewController //通过属性来设置代理对象@property (nonatomic, weak) id delegate;@end实现部分:@implementation LoginViewController- (void)loginButtonClick:(UIButton *)button { // 判断代理对象是否实现这个方法,没有实现会导致崩溃 if([self.delegaterespondsToSelector:@selector(deliverFirsttext:withDeliverSecondtext:)]) { // 调用代理对象的登录方法,代理对象去实现登录方法 [self.delegate deliverFirsttext:self.text1.textwithDeliverSecondtext:self.text2.text]; }}3 Set the proxy (get to the desired object)@interface ViewController () @end@implementation ViewController- (void)viewDidLoad { [superviewDidLoad];}
-(Ibaction) Buttonaction: (ID) Sender {
Secondviewcontroller *VC =
[[Uistoryboard storyboardwithname:@ "Main" Bundle:nil] instantiateviewcontrollerwithidentifier:@ " Secondviewcontroller "];
To set the agent for the current controller SECONDVC
Vc.delegate = self;
[Self PRESENTVIEWCONTROLLER:VC animated:yes completion:^{
}];
}
-(void) Deliverfirsttext: (NSString *) Text1 withdeliversecondtext: (NSString *) text2{
Self.label1.text = Text1;
Self.label2.text = Text2;
}
Implementation results:
iOS proxy mode