自定义代理
答
也许一个例子将帮助您:
#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
这是一个键盘我写的。
VSKeyPadView派生自UIView并具有id<VSKeypadViewDelegate>
类型的代表伊维尔,这意味着设置为delegate
的对象预计符合协议VSKeypadViewDelegate
。该协议有一些必要的和一些可选的方法。这意味着,你有责任编写这些方法 - 对你来说有意义的。
您会在github的示例应用程序中找到此代码。
可能这对你有用。 http://stackoverflow.com/questions/3095642/custom-delegate-example-in-objective-c – 2011-04-15 11:05:36