1 //2 //TouchView.h3 //Lessonuicontrolandsubclass4 5 6 7 #import<UIKit/UIKit.h>8 9 @classTouchView;Ten //1. Develop agreement, agreement name format: class name +delegate One @protocolTouchviewdelegate <NSObject> A - @optional -- (void) Touchbegan: (TouchView *) TouchView; the- (void) touchmoved: (TouchView *) TouchView; -- (void) touchended: (TouchView *) TouchView; -- (void) touchcancelled: (TouchView *) TouchView; - + @end - + @interfaceTouchview:uiview A at //2. Write attribute, property name delegate, type is ID, and abide by protocol < class name delegate> -@property (Assign, nonatomic)ID<TouchViewDelegate>Delegate; - - @end
1 //2 //touchview.m3 //Lessonuicontrolandsubclass4 //5 6 #import "TouchView.h"7 8 @implementationTouchView9 Ten //3. Once the agent is found, let the agent do things One- (void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event { A //determine if delegate has implemented a method - if([_delegate respondstoselector: @selector (Touchbegan:)]) { - [_delegate touchbegan:self]; the } - } - -- (void) touchesmoved: (Nsset *) touches withevent: (Uievent *)Event { + if([_delegate respondstoselector: @selector (touchmoved:)]) { - [_delegate touchmoved:self]; + } A } at -- (void) touchesended: (Nsset *) touches withevent: (Uievent *)Event { - if([_delegate respondstoselector: @selector (touchended:)]) { - [_delegate touchended:self]; - } - } in -- (void) touchescancelled: (Nsset *) touches withevent: (Uievent *)Event { to if([_delegate respondstoselector: @selector (touchcancelled:)]) { + [_delegate touchcancelled:self]; - } the } * $ @end
The simple wording of protocol