Set proxy process for iOS and proxy process for ios
The process of setting proxy for iOS (taking the simulated button as an example)
1. Write Protocol
Create a new file named MyButton, which inherits from UIView and declares the myDelegate protocol in the file.
2. Write protocol Method
Add methods for declared protocols
3. Define a protocol-compliant attribute
The code for the first three steps is as follows:
1 # import <UIKit/UIKit. h> 2 @ class MyButton; 3 // Step 1: Write protocol 4 @ protocol myDelegate <NSObject> 5 // Step 2: Write protocol method (the parameter must be declared above: @ class MyButton;) 6-(void) changeColor :( MyButton *) myButton; 7 @ end 8 9 @ interface MyButton: UIView10 // Step 3: write a myDelegate attribute 11 @ property (nonatomic, assign) id <myDelegate> delegate; 12 13 @ end
4. Write a method to call the Protocol
Method for writing touchesBegan
5. Call Protocol Methods
Step 4:
1 // Step 4 Write the touchesBegan method 2-(void) touchesBegan :( NSSet <UITouch *> *) touches withEvent :( UIEvent *) event {3 // Step 5 call the Protocol Method 4 [_ delegate changeColor: self]; 5}
6. Compliance with the agreement
Enable the class to comply with the Protocol
1 // step 6 follows the Protocol 2 @ interface RootViewController: UIViewController <myDelegate>
7. Set proxy
Self. rootView. myButton. delegate = self;
8. Implementation Protocol
1 // method 2-(void) changeColor :( MyButton *) myButton {3 myButton. backgroundColor = [UIColor colorWithRed: arc4random () % 256/255. 0 green: arc4random () % 256/255. 0 blue: arc4random () % 256/255. 0 alpha: 1]; 4}