I'd like to have a BaseViewController
class (that inherits from UIViewController
) for my project that can act as a base class for other view controllers in my project. This is to add things such as a common progress HUD, a refresh button & network updating mechanism, perhaps an error dialog that can be customized with text by each subclass, etc.
Crucially, I want to be able to add this functionality to both UIViewController
subclasses AND UITableViewController
subclasses, but I'm unable to insert myself into the UITableViewController
class hierarchy, so effectively I need a BaseUIViewController
and a BaseUITableViewController
, but if I do this I have to implement the changes twice and keep them in sync.
What's the best approach to have a single place for common code that has to be accessible both to BaseViewController
subclasses and to BaseTableViewController
subclasses? I only want one place in my code where I would have to deal with common tasks.
1) I've thought about using a category with associative references, but the problem here is that I need to have custom implementation of the ViewController lifecycle classes such as viewDidLoad:, and it's not a good idea.
2) I could skip UITableViewController
entirely, and build up my own BaseTableViewController
from a BaseViewController, implementing tableView
delegate
& datasource
methods myself, but I'm not so keen to skip apple's controller implementation here.
3) I could have a BaseViewController
inheriting from UIViewController
, BaseTableViewController
inheriting from UITableViewController
, and just put custom method calls into a third file ViewControllerBaseMethods
that can be invoked from both 'Base' files. There might still be duplication of properties, but at least the method code would be in one place.
Any other good approaches for this type of thing?