views:

51

answers:

2

This may be a silly question but I haven't found any information on it.

Let's say several of the classes in my program derive from 'MySubView' which is derived from another class, UIViewController.

I would declare it like this:

@interface NewViewController : MySubView {
    // code ...
}
@end

In the future the client wants a change, and desires another view with a table. So I would need to make another class, called MySubTableView, that is a UITableViewController subclassed from MySubView.

I was thinking this would be easier if I could do something like this:

@interface NewViewController : UITableViewController : MySubView {
    // code ...
}
@end

But this doesn't work.

Is there a way to do this with Xcode, or do I have to specifically make the class itself?

EDIT:

I'm not looking for multiple inheritance. A straight inheritance hierarchy would follow:

NewViewController
UITableviewController
MySubView
UIViewController

+2  A: 

No, Objective-C doesn't support declaring those kind of (vertical) inheritance chains. You can only specify the direct super class.

Even if it was possible, there would be problems like calling the correct initializers as they won't be called automatically. Consider a hierarchy like A : B : C - now you can initialize B using e.g. [super init] in As initializer, but how would B know what initializer you want it to call for C?

Georg Fritzsche
That makes a lot of sense. Thanks Georg, and everyone else.
just_another_coder
+1  A: 

Objective-C doesn't support multiple inheritance... But Objective-C programmers rarely miss it, because you can accomplish many of the same tasks using Categories instead. Read up on Objective-C Categories.

Kaelin Colclasure
Categories rock!
just_another_coder
+1 - This is what I would suggest as well. If you want your subclass to basically replace UIView, then unless you are overriding methods, instead create a category of UIView and add in your functions that way. A category is basically a way of adding your own methods to somebody else's class.
Alex Gosselin
The OP doesn't refer to MI, but vertical inheritance. While categories are undoubtedly useful, they don't do the same thing.
Georg Fritzsche