视图的继承 [英] Inheritance of view
问题描述
我是iOS的新手,我想了解该平台的工作原理,所以请问任何愚蠢的问题.
I'm new to iOS and I'm trying to understand how the platform works, so excuse me if I make any dumb questions.
我的问题基于这篇文章中出现的问题:
对视图控制器目标c的继承
My question is based on the problem present in this post:
Inheritance on view controller Objective c
到目前为止,我知道我可以继承控制器,但是我可以继承视图吗?我认为答案是否定的.如果是这样,解决以下问题的最佳方法是什么?
So far, I understand I can inherit controllers, but can I inherit a view? I think the answer is NO. If that is the case, what is the best way to solve the following problem?
我有一个纸牌配对游戏.现在,我必须将游戏扩展为具有类似的纸牌游戏,但具有不同的套牌和规则.
I have a card matching game. Now, I have to expand the game to have a similar card game but with a different deck and rules.
因此,我想重用代码,并有一个主控制器和2个继承的控制器,它们重新定义了牌组的创建.现在我需要有2个不同的视图,因为两个游戏的纸牌数量不同.那么,什么是最好的方法呢?我是否必须将所有控件从一个视图复制到另一个视图并修改所需的内容?
Therefore, I want to reuse the code and have a main controller and 2 inherited controllers that redefine the creation of the deck. Now I need to have 2 different views because the two games have a different number of cards. So, what is the best way to do this? Would I have to copy all the controls from one view to another and modify what's needed?
推荐答案
您可以将任何类型的对象,控制器,视图等子类化 例如:
You can subclass any kind of object, controllers, views, whatever ex:
@interface MyCustomView : UIView
@end
如果两个不同的视图共享很多代码,则不应单独创建它们.他们应该有一个具有共同方法的共同父母,并且两者都继承自这个通用的uiview
If your two different views share a lot of code, then you shouldn't create them separately. They should have a common parent with the common methods and both of them inherit from this generic uiview
这篇关于视图的继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!