简体   繁体   English

子类化您自己的类之一还是使用相同的父类?

[英]Subclassing one of your own classes or using the same parent class?

In my application, I have a mainViewController that has a bunch of letter tile UIImageViews. 在我的应用程序中,我有一个mainViewController,其中有一堆字母拼贴UIImageViews。 You can pan the letters around on this mainViewController to spell out words. 您可以在此mainViewController上平移字母以拼出单词。

I also have popover viewcontrollers that contain letter tiles that you can drag out onto the mainViewController. 我也有包含字母图块的popover视图控制器,您可以将其拖动到mainViewController上。 Once the letter tiles are dropped onto the mainViewController, they need to have access to all the same methods that the letter tiles do that already exist on the mainViewController. 将字母拼贴放到mainViewController上之后,它们需要访问与mainViewController上字母拼贴所具有的所有相同方法。

Currently, I create the letter tiles in the mainViewController and the popover viewcontrollers separately. 目前,我分别在mainViewController和popover viewcontrollers中创建字母图块。 Both the mainViewController and popover viewcontroller's parent classes are UIViewController. mainViewController和popover viewcontroller的父类都是UIViewController。

I was wondering if in my situation, I should be declaring all letter tiles contained in my popover viewcontrollers in the mainViewController class, then have the popoverview controllers be a subclass of mainViewController? 我想知道在我的情况下,我是否应该在mainViewController类中声明我的popover视图控制器中包含的所有字母块,然后popoverview控制器是mainViewController的子类吗?

If I should change it, can you please tell me why it would be better to? 如果我应该更改它,您能告诉我为什么这样做会更好吗?

Thanks! 谢谢!

Objective-C is famous for preferring delegation over subclassing. Objective-C以优先于子类而不是委托而闻名。

I would rather use a datasource class that knows of all the tiles and their methods and use it in both controllers. 我宁愿使用一个知道所有图块及其方法的数据源类,并在两个控制器中使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM