Since OBJECTIVE-C does not support multiple inheritance, it is necessary to use a protocol instead of implementing other classes, so there is a proxy design pattern.
Agent, also known as entrustment, delegation.
The proxy mode enables a single-inherited class to implement methods of classes other than the parent class. Proxies can also be used to pass values.
Let's start with the principle and finally see how the value is passed.
Choose Objective-c file and select protocol to write the method in the protocol file after the file is created.
MyProtocol.h
@protocol mydelegation <NSObject>
-(void) method1;
-(void) method2;
@end
Create another class to implement the methods in MyProtocol.h
MyDelegate.h
#import <Foundation/Foundation.h>
#import "MyProtocol.h"
@interface mydelegate:nsobject<myprotocol>
{
id<myprotocol>obj;
}
-(void) method3;
-(Instancetype) Initwithobj: (id<myprotocol>) obj;
@end
Mydelegate.m
#import "MyDelegate.h"
@implementation MyDelegate
-(Instancetype) Initwithobj: (id<myprotocol>) o{
Self=[super Init];
if (self) {
Obj=o;
}
return self;
}
-(void) method1{
[obj method1];
}
-(void) method2{
[obj METHOD2];
}
-(void) method3{
NSLog (@ "own method ... ");
}
@end
To create a class that implements a method
Helper.h
#import <Foundation/Foundation.h>
#import "MyProtocol.h"
@interface helper:nsobject<myprotocol>
@end
Helper.m
#import "Helper.h"
@implementation Helper
-(void) method1{
NSLog (@ "helper does method1 ... ");
}
-(void) method2{
NSLog (@ "helper does method2 ... ");
}
@end
Proxy implementations:
#import <Foundation/Foundation.h>
#import "MyProtocol.h"
#import "MyDelegate.h"
#import "Helper.h"
int main (int argc, const char * argv[]) {
@autoreleasepool {
helper *h=[[helper Alloc]init];
mydelegate *delegate=[[mydelegate alloc]initwithobj:h];
[delegate method1];
[Delegate method2];
[Delegate method3];
}
return 0;
}
Operation Result:
Helper to do method1 ...
Helper to do method2 ...
Own method ...
The above is the principle of the agent, and then we see how the proxy is passed the value.
Proxy values are passed between classes and classes, as well as passing values between the controller and the controller. I use the value of the controller as an example to show the proxy value.
First create a protocol file
SendDataDelegate.h
#import <Foundation/Foundation.h>
@protocol senddatadelegate <NSObject>
-(void) SendData: (nsstring*) str;
@end
Then create a second view Controller class and drag another view controller in the Main.storyborad and match the class to the view. Add a button to the system-given view controller and connect to the second view controller in a modal manner,
ViewController.h
#import <UIKit/UIKit.h>
#import "SendDataDelegate.h"
@interface viewcontroller:uiviewcontroller<senddatadelegate>
@end
VIEWC ONTROLLER.M
#import "ViewController.h"
#import "SecondViewController.h"
@interface Viewcontroller ()
@property (Weak, nonatomic) Iboutlet Uitextfield *mytextfield;//dragged in from the story version
@property (Nonatomic,strong) NSString *temp;
@end
@implementation Viewcontroller
Remember to name the identifier property of the Go button dragged segue in the story version Gotosecond
-(void) Prepareforsegue: (Uistoryboardsegue *) Segue Sender: (ID) sender{
if ([Segue.identifier isequaltostring:@ "Gotosecond"]) {
Secondviewcontroller *svc= (secondviewcontroller*) Segue.destinationviewcontroller;
svc.delegate=self;
}
}
-(void) Viewdidload {
[Super Viewdidload];
}
-(void) SendData: (NSString *) str{
Self.temp =str;
self.mytextfield.text=self.temp;
}
-(void) didreceivememorywarning {
[Super didreceivememorywarning];
}
@end
SecondViewController.h
#import <UIKit/UIKit.h>
#import "SendDataDelegate.h"
@interface Secondviewcontroller:uiviewcontroller
@property (Nonatomic,weak) id<senddatadelegate>delegate;
@end
Secondviewcontroller.m
#import "SecondViewController.h"
#import "ViewController.h"
@interface Secondviewcontroller ()
@property (Weak, nonatomic) Iboutlet Uitextfield *mytextfield;
@end
@implementation Secondviewcontroller
-(Ibaction) Back: (ID) Sender {
[Self.delegate SendData:self.myTextField.text];
[Self dismissviewcontrolleranimated:yes completion:nil];
}
-(void) Viewdidload {
[Super Viewdidload];
}
-(void) didreceivememorywarning {
[Super didreceivememorywarning];
}
@end
Demonstrate
When a view controller needs another view controller to update the UI or to do something else, we can do so by means of a proxy value.
iOS objective-c Learning Agent design mode