开发者

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

开发者 https://www.devze.com 2023-01-11 15:59 出处:网络
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

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 p开发者_如何学Goerfectly 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消