21

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?

lu yuan
  • 7,207
  • 9
  • 44
  • 78
laverick
  • 714
  • 1
  • 7
  • 23

3 Answers3

8

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

wattson12
  • 11,176
  • 2
  • 32
  • 34
  • the only benefit i can think of is getting code snippets for cellForRow, didSelect etc when you use the Xcode template. but less repetition of code makes up for this – wattson12 Jul 23 '12 at 10:16
  • Right, sounds like rolling my own UITableViewController replacement that inherits off the base UIViewController is the most sensible way to go. – laverick Jul 23 '12 at 11:01
  • 5
    UITableViewController does a lot of things . such as, adjust position automatically when keyboard show, ContentInsets automatically adjust – aelam Nov 26 '13 at 14:49
  • Typo, Should be: self.tableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] style:UITableViewStylePlain]; (not andStyle, at least not with the IOS7 SDK). – John Bowers Dec 20 '13 at 19:30
  • To add to what @aelam said. Storyboard also expects a class which inherits from `UITableViewController`. – Yevhen Dubinin Mar 05 '14 at 10:15
  • 3
    "All UITableViewController does is provide a UITableView property" - That is not true anymore. It does also provide other things like UIRefreshControl. – Toydor May 10 '14 at 10:07
  • @wattson12 Nice answer, though yes, currently tableView has static cells and it's unclear how to workaround it. I'v started a new question regarding this: http://stackoverflow.com/questions/30421038/how-to-recreate-uitableviewcontroller-with-static-cells-support#comment48928874_30421038 – Centurion May 24 '15 at 08:41
  • 3
    `UITableViewController` is required if you want to use Static tableview. – geekay Dec 27 '16 at 07:37
5

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.

Balazs Nemeth
  • 2,333
  • 19
  • 29
  • Thanks. Yes - lack of multiple inheritance is effectively the problem, so having a generalized answer using protocols (rather than my proposed option 2., which I might be able to get away with in this particular case) is very helpful. – laverick Jul 23 '12 at 11:08
  • 1
    @ingaham can you give an example? – Yevhen Dubinin Mar 05 '14 at 10:16
1

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
Chuy47
  • 2,391
  • 1
  • 30
  • 29