views:

59

answers:

3

I have two classes that are already subclasses of a different parent (due to an external library I cannot change this). However I want to share some common code between them, say for example, code that handles an action after a popup dialog etc. How do I do this?

+1  A: 

You can re-factor the appropriate code into a utilities class, and then have the two classes call it. As for the iPhoneSDK, you can probably have the utility method be the delegate method itself.

notnoop
+1  A: 

You could write a category on a common ancestor class. Then both classes could import that Category and call the common code.

Patrick Burleson
I'd be concerned about this - the OP specified that the classes don't have the same parent, so the only "common ancestor" they may have could be NSObject. I wouldn't want to clutter NSObject with specific categories for something like this.
Tim
I agree that putting it on NSObject would potentially be cluttering things, but it is a valid way to do what the OP wants to do without holding an instance of some utility class.
Patrick Burleson
Does this method allow the common code to have access to their private variables as if the code is part of a superclass?
erotsppa
+4  A: 

Refactor the shared code into its own class, and include an instance of that class as a field/property within your other two classes.

David Lively