Duck
Duck

Reputation: 35973

iOS - extending two classes in one

I have extended the UIVIewController class with several functions I want.

I want to add the same functions to the UITableViewController class, I mean, to extend that class too.

In order to do that I have to duplicate all the methods on the extended viewController class to a new pair of files that will be used to extend the tableViewController class. Sorry for my ignorance, but there's a way to extend both classes without that redundancy? I mean, to have just one generic extension class (?) that could be inherited by the viewController and tableViewController extension files? Sorry if this is confuse. What I mean is that instead of this:

UIViewController+extensions.h
UIViewController+extensions.m

both containing a lot of extension methods for the UIViewController class and

UITableViewController+extensions.h
UITableViewController+extensions.m

both containing a lot of extension methods for the UITableViewController class.

I want this:

GenericExtension.h
GenericExtension.m

This, containing the extension methods

then,

UIViewController+extension and UITableViewController+extension, both inheriting GenericExtension, so I have just one copy of the extension methods.

Is that possible?

Upvotes: 0

Views: 1108

Answers (2)

carbonr
carbonr

Reputation: 6067

Option 1:

Write all your methods in (GenericExtension.h/.m) make it of type UIViewController

Later inside UIViewController

@interface RootViewController : Generic Extensions

You can do the same with UITableViewController since UITableViewController is derived from UIViewController.

Upvotes: 0

Eiko
Eiko

Reputation: 25632

If you don't use inheritance but a category on UIViewController (as your file naming suggests), those methods will be available in UITableViewController as well. Make sure to import your header file in your UITableViewController subclass.

Upvotes: 4

Related Questions