我尝试使用协议,但它需要为每两个具有相同内容的类实现“makeMeDraggable”方法。我不希望在每两个类中实现并从共享类继承实现。我如何实现它?
代码:
//--(.h)--//
@interface SharedClass : UIView;
-(void) makeMeDraggable
@end
@interface DraggableUITextView: UITextView
@end
@interface DraggableUIImageView : UIImageView
@end
@implementaion SharedClass
-(void) makeMeDraggable {
//some code
}
@end
@implementation DraggableUITextView
@end
@implementation DraggableUIImageView
@end
//--(.m)--//
TextView *textView = [DraggableUITextView initWithFrame:CGRectMake(0,0,50,50)];
[textView makeMeDraggable];
ImageView *imageView = [DraggableUIImageView imageNamed:@"foo.png"];
[imageView makeMeDraggable];
我想避免以下:
@interface DraggableTextView: UITextView;
-(void) makeMeDraggable
@end
@interface DraggableUIImageView : UIImageView
-(void) makeMeDraggable
@end
@implementation DraggableUITextView
-(void) makeMeDraggable {
//same code...
}
@end
@implementation DraggableUIImageView
-(void) makeMeDraggable {
//same code...
}
@end