找到你要的答案

Q:iOS controllers for custom views

Q:iOS自定义视图控制器

I'm all for trying to create lightweight view controllers (testability, separation of concern, etc. etc.), however, I've been unable to find a reasonable solution or pattern when it comes to certain cases.

A very common case (with plenty of examples out there) is separating the view controller from a tableview's delegate & datasource; I get this, it makes complete sense. But what about cases where a view controller may contain multiple custom views of varying complexity? What should be responsible for controlling each of those views? Surely not just the parent view controller.

I tend to think of a 'UIViewController' as more of a screen controller that is heavily coupled with the UI framework and its events; it does not have a single responsibility of controlling one particular view. To further illustrate my point, imagine a tableview with a couple of different prototype cells - some of which are fairly complicated and may require network access for instance - how should this be managed? Surely no single view controller, datasource or delegate should act as the "controller" for all of these cells? And a lot of that logic/responsibility does not belong in the cell views themselves, so it needs to be delegated somewhere.

One option I've thought of is to just create controller objects (subclasses of NSObject) that act as "view controllers" for the custom views I create, such as a controller object for a complex tableview cell - its single responsibility is to manage that one particular view. The tableview cell then delegates to the controller object, which then (if needed) delegates back to the parent UIViewController. Whilst this will work and helps to separate concerns, it starts to feel a bit awkward with all the layers of delegation going on.

Does anybody have any good suggestions on handling these scenarios or know of good code examples out there that demonstrates this?

Thanks!

我都试图创建轻量级视图控制器(可测试性,分离的关注等),但是,我一直无法找到一个合理的解决方案或模式,当涉及到某些情况下。

一个很常见的情况(用的例子有很多)是从一个TableView的代表及分离视图控制器;数据源;我得到这个,这是有道理的。但是,如果一个视图控制器可能包含多个不同复杂性的自定义视图的情况呢?什么应该负责控制每一个观点?当然不仅仅是父视图控制器。

我倾向于认为“处理”更像一个屏幕控制器与UI框架和事件的强烈耦合;它没有一个特定的角度控制一个单一的责任。为了进一步说明我的观点,想象一个不同的原型细胞,其中一些是相当复杂的,可能需要访问网络的实例-这应该怎么管理TableView?当然,没有一个单一的视图控制器,数据源或代表应作为“控制器”所有这些细胞?很多逻辑/责任不属于细胞视图本身,所以它需要被委派到某处。

我认为一个选项是创建控制器对象(类NSObject)充当“视图控制器”的自定义视图的创建,如单负责复杂的TableView细胞控制器对象是管理一个特定的视图。然后tableview细胞代表的控制器对象,然后(如果需要)代表回到父窗口。虽然这将工作,并有助于分离的关注,它开始感到有点尴尬的所有层的代表团进行。

有人有任何好的建议,处理这些情况或知道良好的代码示例,在那里演示了这一点?

谢谢!

ios  uiviewcontroller  separation-of-concerns