Custom delegate

匿名 (未验证) 提交于 2019-12-03 02:31:01

问题:

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
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!