I want to know how can I use a custom delegate?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Maybe an example will help you:
#import <UIKit/UIKit.h> @protocol VSKeypadViewDelegate @required -(int)numberOfRows; -(int)numberOfColumns; -(NSString*)titleForButtonOnRow:(int)row andColumn:(int)column; -(id)valueForButtonOnRow:(int)row andColumn:(int)column; -(CGSize)sizeForButtonOnRow:(int)row andColumn:(int)column; -(void)receivedValue:(id)value; -(CGPoint)keypadOrigin; @optional -(NSArray *)additionalButtonsForKeypad; //-(UIColor *)keypadBackgroundColor; //-(UIColor *)keyBackgroundColorForRow:(int)row andColumn:(int)Column; -(UIImage *)backgroundImageForState:(UIControlState)state forKeyAtRow:(int)row andColumn:(int)column; -(BOOL)isButtonEnabledAtRow:(int)row andColumn:(int)column; @end @interface VSKeypadView : UIView { id<VSKeypadViewDelegate> delegate; NSArray *keypadButtons; } + (VSKeypadView *)keypadViewWithFrame:(CGRect)r; - (id)initWithFrame:(CGRect)r ; -(void)fireKeypadButton:(id)sender; @property(nonatomic, assign) id<VSKeypadViewDelegate> delegate; @end This is a keypad I wrote.
The VSKeyPadView derives from UIView and has a delegate ivar of type id<VSKeypadViewDelegate>, that means, that the object set to delegate is expected to conform to the protocol VSKeypadViewDelegate. That protocol has some required and some optional method. That means, that it is your responsibility to write those method ― what ever makes sense for you.
You will find this code running in an example application at github.
文章来源: Custom delegate