Set proxy process for iOS and proxy process for ios

Source: Internet
Author: User

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}

 

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.