views:

179

answers:

3

I have some functionality that I need in all my classes which derive from either UIView or UIImageView.

More specifically, I have gesture related code that both these classes need.
Currently my implementation is:

UIGestureView : UIView
UIGestureImageView : UIImageView

and make all classes derive from these. These classes will again contain methods that the derived class will implement.

My problem is that the gesture handling code is duplicated in UIGestureView and in UIGestureImageView.

The natural thing here (as a c++ programmer) would be to use multiple inheritance

UIGestureView : UIView, GestureHandler
UIGestureImageView : UIImageView, GestureHandler

and let GestureHandler perform all the generic work but as far as I have understood this is not possible.

What is the objective-c way of doing this (without too many levels of child calling parent etc.)?

Just to stress, the problem is how to avoid implementing the same code twice, once in UIView (or its derived class) and again for UIImageView (or its derived class).

+4  A: 

You could extend UIView with your gesture handling, and then every objects that inherits from UIView will have the methods you want. Not quite as awesome and subclassing, but would work on the global scope.

@interface UIView (GestureHandling)

- (void)didMoveAFinger:(UITouch*)touchOrWhatever;
// etc., etc.

@end

I have had similar issues with UIViewController and UITableViewController. I have a subclass of one that I want to share code with the subclass of the other. Yet there is no common place to inject that code if you want a subclass. The alternative is categories on the common superclass.

Squeegy
This still doesn't solve the problem with code duplication as I need the same behaviour in UIImageView.
LK
@LK: UIImageView is a subclass of UIView.
Chuck
Ok - just saw the same in the another answer. will check this out - thanks!
LK
@LK: Chuck is right. Any methods you add to `UIView` will be accessible on all it's subclasses, including `UIImageView` as well as dozens of other classes in the SDK.
Squeegy
A: 

You should try using protocols and delegation. They solve the multiple inheritance problem in objective-c.

@protocol MyProtocol <NSObject>
//Method declarations go here
@end

@protocol MyProtocol2 <NSObject>
//Method declarations go here
@end

@interface CustomView : UIView <MyProtocol, MyProtocol2>
JeremySpouken
Protocols don't solve multiple inheritance, as they contain no code. They do provide a template for common interfaces between unrelated classes, but do not let you do something like handle gesture code. You still need to put that code somewhere.
Squeegy
Protocols/delegates just seem to add an additional layer for calling back - I am trying to avoid this
LK
+1  A: 

Check out categories: http://macdevelopertips.com/objective-c/objective-c-categories.html

Seva Alekseyev
As the other answer here, this allows adding code to an existing class, but does not solve the problem with code duplication, as I need the exact same code to exist in both UIView and in UIImageView
LK
A category on UIView should solve your issue, since UIImageView inherits from UIView.
kubi
oh - I see, will check that out - thanks.
LK