子类化还是不子类化:那是......?
我正在构建一个具有许多视图和子视图的应用程序。 我试图弄清楚如何创建子类。
一个简单的例子: 我有一个带有头部视图和页脚视图的表格视图。 (我的观点比这个观点复杂得多,但我试图用一个简单的观点来解释我的观点)。
一开始,我不想为每个视图创建视图子类,但我发现我的代码变得越来越复杂并且难以导航。
然后我发现自己为主视图中的每个子视图创建了一个视图。事实上,我更喜欢它,因为我的代码对我来说更清晰。唯一的问题是我不知道每种方法的缺点和优点是什么。
您如何决定何时创建自定义视图或在主视图中创建其代码?
谢谢
I am building an app with many views and subviews.
I am trying to figure out how to approach creation of the subclass.
one simple example:
I have a table view with heads view and footer view. (I have much more complex views then this one, but I am trying to explain my point with a simple one).
In the beginning I didn't want to create views subclasses for every view, but I found that my code is getting complex and hard to navigate in.
Then I found my self creating a view for every subview in the main view. The truth is that I like it better cause my code is clearer to me. Th only thing is that I don't know what are to cons and pros of each approach.
How do you decide when to create a custom view or create it's code in the main view?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我通常仅在存在最好在此类中完成的特定功能时才创建 UIView 子类。这可能是视图是一个控件,例如用于输入值的键盘或
UITableViewCell
子类。否则,我通常不会子类化
UIView
,而只是在 Interface Builder 或代码中创建视图层次结构,然后使用UIViewController
子类来执行所有业务逻辑。基本上,一般来说,在 iOS 编程中,您希望在 UIViewController 子类中拥有“视图”(或屏幕,或任何您想要调用的名称)的所有逻辑。如果不完全理解你的层次结构,就很难真正给出好的建议。
I usually create a
UIView
subclass only when there is specific functionality that would be best done in such a class. This might be that the view is a control such as a keyboard to enter a value or aUITableViewCell
subclass.Otherwise I would generally not subclass
UIView
but just create the view hierarchy in Interface Builder or in code and then use theUIViewController
subclass to do all the business logic.Basically, in general with iOS programming, you want to have all the logic of how a "view" (or screen, or whatever you want to call it) in a
UIViewController
subclass. It's hard though to really give good advice without fully understanding your hierarchy.