iOS proxy mode

Source: Internet
Author: User

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

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.