Common View Controller Base Class

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?

Answers


All UITableViewController does is provide a UITableView property, declare itself as the delegate and data source, and set its view as the table view

so create a BaseTableViewController which extends BaseViewController, declare it as a UITableViewDataSource and UITableViewDelegate and implement the required methods, add a UITableView property and implement loadView:

- (void)loadView
{
    self.tableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] andStyle:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.datasource = self;
    self.view = self.tableView;
}

if you want to use different table view styles, you can provide a common method or property to take the table view style from when initialising


I think, you should create a protocol definition with your common properties and functions and your BaseTableViewController and BaseViewController must adopt it. After it, your third options sounds good, you should create a file (ViewControllerBaseMethods) with the implementation of the protocol, that should be invoked from both 'Base' files.

So the method code would be in one place and there are no multiple properties declaration.

This is the way to address the absence of multiple inheritance in Objective-C.


In Swift you can create a UIViewController extension and put your methods there, this way you can use them from UIViewControllers and UITableViewControllers.

extension UIViewController {
    var aComputedProperty: Bool {
        return true
    }

    func aMethod() {
        //do something
    }
}

The disadvantage of this is that you won't be able to use stored properties.

var aProperty: Int? //this is not allowed in extensions

Need Your Help

How to get "remainder" of query (all non-matched pairs) in LINQ and collect it in a list

c# linq

I am matching requests with responses based on requestId as follows:

Xpath "ends-with" does not work

xpath ends-with

I am trying to find an input element with dynamic id name always ending with "register". So far I tried this