Simulate multiple inheritance in Objective-C
architecture, inheritance, multiple-inheritance, objective-c
Solution
This is one suggestion, but I don't know how useful it is because I don't know your code.
You could implement a bunch of methods in a category for the UIViewcontroller class. For example:
@implementation UIViewController (myCategory)
- (void)setupColors;
...
@end
Since both MyViewcontroller and MyTableViewcontroller inherit from UIViewController, they would inherit your methods.
The only thing that you would copy in both implementation is the invocation of those functions, but the duplicate code would be much less.
- viewDidLoad...
{
[self setupColors];
}
Just be careful if you override methods, because you can't call [super ... ] on a category as you can in an inherited class
Problem
I have kind of an abstract class for my `UIViewControllers` (lets call it `MyViewController`) which overrides some basic methods like `viewDidLoad` or `viewDidDisappear`. In this methods some preparations are made, like setting up colors for the navigation bar, or preparing the bar buttons or something like that. Now I want this basic behaviour for my `UITableViewControllers` also. So I made a new class that inherits `UITableViewController` (lets call it `MyTableViewController`) and copied 99% of the code from `MyViewController`. In this image you see my current architecture. Listed are the overriden methods, in which other private methods are called. Again, `MyViewController` and `MyTableViewController` share 99% codebase (only difference is the name of the class and the super class). For obvious reasons this is crap. Is there an elegant solution to make `MyTableViewController` a subclass of both `MyViewController` and `UITableViewController`?