Ali
Ali

Reputation: 1436

How to not duplicate code in Objective-C? (a.k.a. multiple inheritance)

When you have an UIViewController and UITableViewController classes and you wanted to let these two do some common stuff in their - (void)viewDidLoad how could you achieve this in Objective-C without actually duplicating your code?

I tried to create MyUIViewController inheriting UIViewController and implement viewDidLoad in there. This perfectly works with UIViewController classes obviously, but won't work in UITableViewController, since I can't simply replace @interface MyTableViewController : UITableViewController with @interface MyTableViewController : MyUIViewController.

I believe this topic is about "multiple inheritance" in Objective-C language, but other than figuring out what's different in Objective-C, I'd really like to know how to do guys do such thing?

Upvotes: 4

Views: 1000

Answers (1)

jtbandes
jtbandes

Reputation: 118731

This thread has some good information. One of your main options is to make a class with that shared functionality and hold it as an instance variable, then forward messages to it in forwardInvocation.

Upvotes: 1

Related Questions