Objective-c Commission, informal agreement, formal agreement

Source: Internet
Author: User

First, the concept:
1. Delegate (delegate, also called agent): When an object receives an event or notification, it queries its delegate object whether it can respond to the event or notification, and if so, This object sends a message to its delegate object (executes a method call). With this mechanism, you can avoid the modification of complex objects by placing your own custom code in the delegate object without subclasses and method overloading. When an event of interest occurs, a complex object sends a message to your custom delegate object. This "Hook" allows you to execute your own custom code to achieve the desired behavior.
2, informal agreement (informal protocol): using category categories to achieve, informal protocol is a category of nsobject, so that any class object can be used as a delegate object, it can list all the methods the object can execute, so as to implement the delegate, We can use selectors to determine if this method is available in the informal protocol.
3, formal agreement (formal protocol): is a named method list, compared to the informal protocol, it requires the display of the adoption of the Protocol, the method of adopting the protocol is to list the name of the protocol in the @interface declaration of the class, at this time, the implementation of the Protocol class should abide by the protocol, Commit to implement all the methods in the protocol, or the compiler will issue a warning.
Formal protocol protocol is similar to C + + pure virtual function, the protocol is only declared, not implemented, used in subclasses, the method in the protocol has two kinds of properties, @required (default) and @optional two, @required the property requires the implementation of the Protocol class must implement this method, The @optional property method does not require that if the protocol is not determined to be implemented, it can be judged using the Respondstoselector: @select ().

Ii. Examples of application

The source code is as follows:

/********* Simple Delegate Example ************/

@interface A:nsobject

-(void) Log;

@end

@implementation A

-(void) log{

NSLog (@ "This is A Log");

}

@end

@interface b:nsobject{

A * delegate;

}

@property (Nonatomic,retain) A * delegate;

-(void) calllog;

@end

@implementation B

@synthesize delegate;

-(ID) init{

if (self = [superinit]) {

delegate = [[Aalloc]init];

}

returnself;

}

-(void) calllog{

NSLog (@ "This is B calllog");

[Self.delegatelog];

}

@end

/********* Simple Delegate Example ************/

/********* Simple Formal Agreement example ************/

Protocol--begain

@protocol MyProtocol

An optional implementation of the

@optional

-(void) optional_print;

Requirements to achieve

@required

-(void) required_print;

@end

Protocol--end

Class--begain using the protocol

@interface myclass:nsobject<myprotocol>

-(void) print;

@end

@implementation MyClass

-(void) print{

NSLog (@ "This is myClass print");

}

Implementing an optional Implementation protocol

-(void) optional_print{

NSLog (@ "This is Protocol Optional_print");

}

Implementing the protocols that must be implemented

-(void) required_print{

NSLog (@ "This is Protocol Required_print");

}

@end

Class--end using the protocol

/********* Simple Formal Agreement example ************/

/********* Simple Informal Protocol example ************/

@interface NSObject (mycategory)

-(void) informal_protocol_print;

@end

@implementation NSObject (mycategory)

-(void) informal_protocol_print{

NSLog (@ "This is Informal_protocol_print");

}

@end

/********* Simple Informal Protocol example ************/

/********* formal Agreement Implementation delegate example ************/

Protocol--begain

@protocol Myprotocolex

An optional implementation of the

@optional

-(void) optional_print;

Requirements to achieve

@required

-(void) required_print;

@end

Protocol--end

@interface myclassex:nsobject{

Id<myprotocolex> delegate;

}

@property (nonatomic,assign) id<myprotocolex> delegate;

-(void) print;

@end

@implementation Myclassex

@synthesize delegate;

-(void) print{

NSLog (@ "This is myclassex print");

[Self.delegateoptional_print];

[Self.delegaterequired_print];

}

@end

@interface mycall:nsobject<myprotocol>{

Myclassex *cls;

}

@property (Nonatomic,retain) myclassex *cls;

-(void) callprint;

@end

@implementation Mycall

@synthesize CLS;

-(ID) init{

if (Self=[superinit]) {

myclassex* c = [[Myclassexalloc]init];

Self.cls = C;

Cls.delegate = (id) self;

[Crelease];

}

returnself;

}

-(void) callprint{

NSLog (@ "This is Mycall callprint");

[Self.clsprint];

}

-(void) optional_print{

NSLog (@ "This is mycall implemented formal protocol Optional_print");

}

-(void) required_print{

NSLog (@ "This is mycall implemented formal protocol Required_print");

}

@end

/********* formal Agreement Implementation delegate example ************/

int main (int argc,char *argv[])

{

@autoreleasepool {

Use of Delegates

b* B = [[B alloc]init];

[Bcalllog];

[B informal_protocol_print];//use of informal agreements

NSLog (@ "---------------------------------");

Use of the Protocol

MyClass *cls = [[Myclassalloc]init];

[Clsprint];

[Clsoptional_print];

[Clsrequired_print];

[Use of the CLS informal_protocol_print];//Informal protocol

NSLog (@ "---------------------------------");

Formal agreements for the use of delegates

Mycall *call = [[Mycallalloc]init];

[Callcallprint];

NSLog (@ "---------------------------------");

Return Uiapplicationmain (argc, argv, Nil, Nsstringfromclass ([Appdelegate class]));

}

}

http://blog.csdn.net/jjunjoe/article/details/7846025

Objective-c Commission, informal agreement, formal agreement

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.