protocol [' pr?ut?k?l] (example:http://blog.sina.com.cn/s/blog_6aafe9c90100yozz.html )
I. Description
two classes for communication, with the protocol is more convenient.
1. The protocol declares a method that can be implemented by any class
2. The protocol is not a class, it defines an interface that other objects can implement
3. If a method in the protocol is implemented in a class, that is the class that implements the protocol.
4. The protocol is often used to implement a delegate object. A delegate object is a special object that is used to coordinate or represent other objects.
5: The delegate, is to call their own definition methods, other classes to implement.
6. Description of new features
@optional Precompiled directives: represents a method that can be selected for implementation
@required precompiled directives: means that the implementation must be enforced
second, the definition
. h
@protocol contactctrldelegate-(void) Dismisscontactsctrl; @end @interface Contactsctrl:uiviewcontroller {
. M
Iii. Examples
 
@protocol uitextviewdelegate <NSObject> @optional-(BOOL) textviewshouldbeginediting: (Uitextview *) TextView; -(BOOL) textviewshouldendediting: (Uitextview *) TextView; -(void) textviewdidbeginediting: (Uitextview *) TextView; -(void) textviewdidendediting: (Uitextview *) TextView; -(BOOL) TextView: (Uitextview *) TextView Shouldchangetextinrange: (nsrange) Range Replacementtext: (NSString *) text; -(void) Textviewdidchange: (Uitextview *) TextView; -(void) Textviewdidchangeselection: (Uitextview *) TextView; @end
if you want to invoke these methods, you must set the Uitextview delegate: textview.delegate = self;
Four, Demo
1, ContactsCtrl.h
#import <UIKit/UIKit.h>//Definition protocol @protocol contactctrldelegate-(void) Dismisscontactsctrl; @end @interface Contactsctrl:uiviewcontroller { iboutlet uinavigationbar *contactnavbar;
2, CONTACTSCTRL.M
@implementation Contactsctrl @synthesize Delegate; Implement Viewdidload to does additional setup after loading the view, typically from a nib. -(void) viewdidload { [super viewdidload]; ContactNavBar.topItem.prompt = @ "Select a contact to send a text message"; }//method in the calling protocol-(ibaction) CANCELBTN: (ID) sender{
3, ProtocolDemoCtrl.h
#import <UIKit/UIKit.h> #import "ContactsCtrl.h" @interface Protocoldemoctrl:uiviewcontroller < contactctrldelegate>{//Adding a delegate
4, PROTOCOLDEMOCTRL.M
#import "ProtocolDemoCtrl.h" #define BARBUTTONADD (SELECTOR) [[[Uibarbuttonitem alloc] Initwithbarbuttonsystemitem: Uibarbuttonsystemitemadd target:self Action:selector] autorelease]; @implementation Protocoldemoctrl @synthesize Contactsview; Implement Viewdidload to does additional setup after loading the view, typically from a nib. -(void) viewdidload {[Super viewdidload]; Self.navigationItem.rightBarButtonItem = Barbuttonadd (@selector (addcontactaction:));}-(void) Addcontactaction: (ID ) sender{Contactsctrl *contactview = [[Contactsctrl alloc] initwithnibname:@ "Contactsctrl" bundle:nil]; Self.contactsview = Contactview; Contactsview.delegate = self;//Set delegate [self Presentmodalviewcontroller:contactsview animated:yes]; [Contactview release]; }//Implement the method in the Contactctrldelegate protocol-(void) dismisscontactsctrl{[Contactsview Dismissmodalviewcontrolleranimated:yes];}
In summary, if the Class A. h file defines a protocol, within class A We also need to define a delegate delegate, which has a MethodA method that must be implemented (@required, which is implemented in the class that implements the Protocol), This MethodA method needs to be called in a. m file of Class A, and we use delegates to call this MethodA method
Class B implements this protocol, defines the object A of Class A in the. h file, needs to implement the MethodA method in the. m file, and needs to set the delegate
A.delegate = self;//Set Delegate
If MethodA returns an object of Class B, then Class B properties and methods can be called in Class A, and A's properties and methods can also be called in Class B.