How to create an agent yourself in objective-c

Source: Internet
Author: User

First of all we need to create a class, in order to facilitate understanding of the real life of the simulation to find intermediary rent house, so first create a person class

1. Define the proxy protocol in Person.h and complete the proxy method declaration: The code is as follows

1 //Person.h File2 3 #import<Foundation/Foundation.h>4 5 @classPerson ;6 7 //1 Defining an agent's protocol8 @protocolPersondelegate <NSObject>9 Ten //Optional Methods One @optional A- (void) Personfindhouse: (Person *) person; - //Necessary Methods - @required the  - @end -  - @interfacePerson:nsobject +  - @end

Note: The optional method in the proxy protocol can be implemented without implementation, but the necessary method must implement

2. Define proxy Properties: Code as follows

1 //Person.h File2  3 #import<Foundation/Foundation.h>4 @classPerson ;5 //1 Defining an agent's protocol6 @protocolPersondelegate <NSObject>7 //Optional Methods8 @optional9- (void) Personfindhouse: (Person *) person;Ten //Necessary Methods One @required A  - @end -   the @interfacePerson:nsobject -@property (nonatomic,copy) NSString *name; - //2 defining Proxy Properties -@property (Nonatomic,weak)ID<PersonDelegate>Delegate; +- (void) Zufang; - @end

3, call the Proxy method (notification) to send a message to the agent: The code is as follows

1 //person.m File2 #import "Person.h"3 4 @implementation Person5- (void) Zufang6 {7NSLog (@"%@--to rent .", self.name);8     9     //3 Calling the Proxy method (notification) to send a message to the agentTen     if([Self.Delegaterespondstoselector: @selector (personfindhouse:)]) One     { A[Self.Delegatepersonfindhouse:self]; -  -     } the } - @end

The 10th line of the above code is to determine whether the object implements the Personfindhouse: This method

OK now we need to define a Zhongjie class to use this proxy

1, let this class abide by the agent agreement: The code is as follows

1 // ZhongJie.h File 2 #import <Foundation/Foundation.h>3#import"Person.h" 4 5 @interface Zhongjie:nsobject <PersonDelegate>67@end

2, implement the Proxy method: The code is as follows

1 //zhongjie.m File2 #import "ZhongJie.h"3 4 @implementationZhongjie5 6- (void) Personfindhouse: (Person *) Person7 {8NSLog (@"We found the house.");9 }Ten  One @end

3, set the agent properties: The code is as follows

1 //viewcontroller.m File2 #import "ViewController.h"3 #import "Person.h"4 #import "ZhongJie.h"5 @interfaceViewcontroller ()6 7 @end8 9 @implementationViewcontrollerTen  One- (void) Viewdidload { A [Super Viewdidload]; -     //additional setup after loading the view, typically from a nib. -Zhongjie *ZJ =[[Zhongjie alloc] init]; the      -Person *p =[[Person alloc] init]; -      -P.name =@"SomeOne"; +     //3 Setting Proxy Properties -P.Delegate=ZJ; +      A [P Zufang]; at      - } -  -  - @end

So we created the proxy and let an object use the proxy.

How to create an agent yourself in objective-c

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.