Objective-C learning document protocol usage

Source: Internet
Author: User

The protocol usage in Objective-C learning document is described in this article, mainly to understand and learn about the Protocol problems in Objective-C. For details, refer to the content of this article.

I. Protocol definition

 
 
  1. @protocol test  
  2. -void) testpocol:int)t;  
  3. @end 

Ii. Protocol inheritance

H header file

 
 
  1. # Import "test. h" // import Protocol
  2. @ Interface testViewController: UIViewController <test> {
  3. // Id <test> testp;
  4. }

M implementation file

 
 
  1. @ Implementation testViewController
  2. -Void) viewDidLoad {
  3. [Super viewDidLoad];
  4. // Class that calls the implementation method
  5. Testdiaoyong * td = [[testdiaoyong alloc] init];
  6. Td. testd = self; assign the Class Object of the current implementation protocol to the desired place
  7. // You can also use the following method to transfer the Protocol
  8. [Td setpoco: self]
  9. }
  10. -(Void) testpocol :( int) s {
  11.  
  12. NSLog (@ "testpocol ...... % d", s );
  13. }
  14. @ End

Iii. Call Protocol

 
 
  1. @ Interface testdiaoyong: NSObject {
  2. Id <test> testp;
  3. }
  4. -(Void) setpoco: t;
  5. -(Void) setlen;
  6. @ End
  7. @ Implementation testdiaoyong
  8. @ Synthesize testp;
  9. // Protocol t can bear the type
  10. -(Void) setpoco: t {
  11. Self. testp = t;
  12. }
  13. -(Void) start {
  14. [Testp testpocol: 99];
  15. }
  16.  
  17. @ End

Summary:Objective-CLearning documentProtocolThe content of the usage method has been introduced. I hope this article will help you!

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.