存在重叠对象时如何设计代码架构? [英] How to design code architecture when overlapping objects exist?

查看:16
本文介绍了存在重叠对象时如何设计代码架构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 1 个主主屏幕 (MVC) 和 4 个其他屏幕,它们共享约 80% 的图形对象.它们在一些标签文本、带有操作的按钮和其他一些 20% 的图形方面有所不同.考虑到干净的面向对象代码架构,我已经开始将这些屏幕实现为单独的 UIViewController.但是我不喜欢有指向那 4 个 MVC 的指针和一些方法的重复,所以我将代码重写为一个 UIViewController.大多数图形对象都是我在 Interface Builder 中使用的 UIImageViews.所以,现在那个统一"的 UIViewController 的 xib 文件有点臃肿,因为来自这 4 个屏幕的重叠对象.我还需要像 showScreen1、hideScreen2、showScreen2、hideScreen2 这样的方法和其他方法,所以我对这个架构也不是很满意.我知道我不应该混合这 4 个 MVC(屏幕),并且代码和 xib 文件会更干净,但我也关注性能、节省内存负载等.您对此有何看法?加载单独的 UIViewController 需要多少内存资源?也许与代码清洁度相比,节省的内存量不值得?同样,拥有 4 个单独的 MVC 将迫使我有 4 个指向我的主 MVC 的指针和 4 个指向我的主 MVC 的那些单独 MVC 的指针,因为我需要在这些屏幕之间进行通信和导航.另一种选择是使用通知而不是指针,但它不会改变所需关系的数量.所以请分享您的想法和见解:)

I have 1 main Master screen (MVC) and 4 other screens that share ~80% of graphic objects. They differ in some label texts, a button with action and some other 20% graphics. Thinking in terms of clean object oriented code architecture, I have started to implement those screens as separate UIViewControllers. But I didn't like having pointers to those 4 MVCs and a duplication of some methods, so I rewrited the code into one UIViewController. Most of graphic objects are UIImageViews that I put on with Interface Builder. So, now the xib file of that "unified" UIViewController is a little bit bloated with overlapping objects from those 4 screens. I also need to have those methods like showScreen1, hideScreen2, showScreen2, hideScreen2, and other methods, so I'm not very happy with this architecture too. I understand that I should not mix those 4 MVCs (screens) and both the code, and xib files would be cleaner but I'm also thing about performance, saving memory loads and etc. What would be your opinions on that? How much memory resources takes the loading of separate UIViewController? Maybe the amount of saved memory is not worth when compared to code cleanness? Again, having 4 separate MVC's would force me to have 4 pointers both to my Master MVCs and 4 pointers to those separate MVCs from my Master MVC, because I need to communicate and navigate between those screens. Another option would to use notifications instead of pointers but it doesn't change the amount of required relations. So please share your thoughts and insights :)

推荐答案

对于共享相同设计的用户界面控件,也许您可​​以使用类别,这就是我在项目中所做的.我已经向 UINavigationBar 添加了一个类别以在栏上显示纹理,向 UIBarButtonItem/UIButton 添加一个类别以用于返回 &取消按钮,因为我的 ViewController 都共享相同的背景图像,所以我创建了一个 UIViewController 子类,其中包含默认背景图像 - 我的应用程序中使用的每个视图控制器都继承自该默认视图控制器.

For user interface controls that share the same design perhaps you could use categories, this is what I'm doing in a project. I've added a category to UINavigationBar to show a texture on the bar, added a category to UIBarButtonItem / UIButton for a back & cancel button and since my ViewControllers all share the same background image I've created a UIViewController subclass that contains the default background image - every view controller used in my app inherits from this default view controller.

我会坚持每个屏幕使用 1 个视图控制器.

I would stick with 1 view controller per screen.

Apple 不建议创建巨型笔尖,这一点在 WWDC 2011 的故事板视频中尤为明显(如果您是注册的 Apple 开发人员,请查看此视频).巨大的笔尖在性能方面很糟糕,加载所有对象需要花费大量时间,并且笔尖中的所有对象始终保留在内存中,即使这些对象位于当前未显示的屏幕上.每个屏幕有一个单独的笔尖是更好的方法.

Apple doesn't recommend creating giant nibs, this is especially visible in the WWDC 2011 video on storyboarding (check this video out if you're a registered Apple developer). Giant nibs are bad in terms of performance, it takes a lot of time to load all objects and all the objects from the nib stay in memory all the time, even if the objects are on a screen that's currently not being displayed. It's a better approach to have a separate nib per screen.

这篇关于存在重叠对象时如何设计代码架构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆