1, the protocol uses the situation: A class has an instance method, this instance method to the object passed over, restricts it must implement a specific method, and the passed object type is not limited.
2. Format
Protocol name: @protocol protocol Name Protocol name general: class name +delegate.
Protocol content: A specific method
Specific examples:
①
The instance method in the AA class has a protocol limit.
The BB class is a generic class.
Ii
In Aa.h :
Protocol wording
@protocol aadelegate < NSObject >
Agreement content
-(void) play;
@end
@interface Aa: NSObject
The object of the infection complies with the agreement
-(void) Playwithobject: (ID<AaDelegate>) anyobject;
@end
An example method of AA.M with protocol is written in the paper.
-(void) Playwithobject: ( ID <aa Delegate >) anyobject{
[anyobject play];
}
③
When introducing the AA Bb class in the Viewcontroller.
Create objects individually. AA class object, invoking an instance method to pass the BB class object into the Times warning: The BB class object must be compliant with the protocol of the instance method in the AA class.
Need to Bb.hin the Bb class:
The class in which the Protocol is introduced
#import "Aa.h"
Let the BB class comply with the agreement:
@interface Bb: NSObject <aaDelegate>
Implement the Protocol content in Bb.h.
This article is from the "Get" blog, so be sure to keep this source http://3517465.blog.51cto.com/3507465/1691280
Objective-c Agreement Lan Yi Education