OC Protocol Protocol

Source: Internet
Author: User

I. Overview
    • A protocol is a delegate (delegate)/proxy, which is an object that provides an opportunity to respond to changes in another object or to affect the behavior of another object.
    • A protocol can only be used for one purpose, that is, to declare a heap of methods for an inherited class.
    • The base class adheres to the Protocol, and its derived class also adheres to the protocol that its base class adheres to, meaning that the parent class's protocol can inherit from the quilt class.
    • In OC, the class cannot inherit multiple, only single inheritance, but the protocol can inherit more.
    • The method of the Protocol Declaration can be implemented by any class that inherits.
    • Agreement <NSObject> is the base agreement for all agreements.
    • A protocol can comply with an agreement, which means that one protocol can comply with (inherit) other agreements, so that it can have declarations of all methods of other protocols.
    • There are also two keywords in the protocol @optional and @required, @optional modified method declaration tells the developer that this method can be implemented, or not be implemented. @required decorated method declaration tells the development that this method must be implemented, of course, the two keyword modification methods can be ignored, but Xcode will give a warning.
II. Protocol Definition Format
1 // protocol name defined by protocol name 2 @protocol MyProtocol <NSObject>3@required45@optional67  @end

Iii. examples
1 //entrusted by DemoViewController.h2 #import<UIKit/UIKit.h>3 4 @protocolDemodelegate <NSObject>5- (void) Seteditwithvalue: (NSString *) Avalue;6 @end7 8 @interfaceDemoviewcontroller:uiviewcontroller9 {Ten     ID<DemoDelegate>idelegate; One } A  - //This delegate is the Setdelegate method -@property (nonatomic, assign)ID<DemoDelegate>idelegate; the  - @end -  - //DEMOVIEWCONTROLLER.M + #import "DemoViewController.h" -  + @interfaceDemoviewcontroller () A { atNSString *ivarstring; - } -  -@property (nonatomic, copy) NSString *ivarstring; -  -- (void) print; in  - @end to  + @implementationDemoviewcontroller - @synthesizeidelegate; the @synthesizeivarstring; *  $- (void) PrintPanax Notoginseng { -[Self.idelegate Seteditwithvalue:@"Test"]; the } +  A @end the  +  - //To be trusted, be aware that the Code manager object is to be set in the delegate $ //DemoController.h $ #import<Foundation/Foundation.h> -  - #import "DemoViewController.h" the  - @interfaceDemocontroller:nsobject <DemoDelegate>Wuyi  the @end -  Wu //DEMOCONTROLLER.M - #import "DemoController.h" About  $ @interfaceDemocontroller () - { -Demoviewcontroller *Idvcontroller; - } A  +@property (nonatomic, retain) Demoviewcontroller *Idvcontroller; the  --(Instancetype) Initwithvalue: (NSString *) Avalue; $  the @end the  the @implementationDemocontroller the @synthesizeIdvcontroller; -  in-(Instancetype) Initwithvalue: (NSString *) Avalue the { the     if(self =[Super init]) About     { theSelf.idvcontroller =[[Demoviewcontroller alloc] init]; the [Self.idvcontroller setidelegate:self]; the     } +      -     returnSelf ; the }Bayi  the- (void) Seteditwithvalue: (NSString *) Avalue the { -     // to do -NSLog (@"Detail Page Edit value:%@", avalue); the } the  the @end

PS: The proxy object that is set in the delegate is the member property of the delegate, namely: the Getter and setter method @property delegate.

1 @interface Demoviewcontroller 2 {3   ID<DemoController> idelegate;   4 }56ID<DemoController> idelegate;

OC Protocol Protocol

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.