视图控制器包含与直接视图操作

发布于 2024-12-29 18:03:27 字数 369 浏览 3 评论 0原文

我正在开发一个具有以下视觉结构的 iOS 5 应用程序:

view containsment

其中每个方块都是一个单独的视图。蓝色视图将动态创建(它们的数量、子视图、响应事件背后的逻辑)。我想知道哪种方法更好:

  • 创建 BlueViewController 的不同实例并将它们作为子视图控制器添加到 rootViewController 或
  • 简单地将不同的视图添加到视图层次结构而不创建 BlueViewController 类并直接通过 rootViewController 操作视图

有什么优点和两种方法的缺点?

I am developing an iOS 5 application with the following visual structure:

view containment

where each square is a separate view. The blue views will be created dynamically (their number, the subviews, the logic behind of responding to the events). I was wondering which approach is better:

  • Creating different instances of BlueViewController and adding them as child view controllers to the rootViewController or
  • simple adding different views to the view hierarchy without creating the BlueViewController class and manipulating the views directly via the rootViewController

What are the pros and cons of both approaches?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情话难免假 2025-01-05 18:03:27

这完全取决于他们做什么。如果它们只是视图,那么您应该只将视图添加到视图层次结构中。例如,如果它们只是显示某种信息,那么您所需要的只是一个视图。如果您这样做,我仍然建议您使用自定义 UIView 子类来处理其中的黄色视图和布局。

但是,如果您发现它们每个都需要执行大量“控制器逻辑”,例如处理复杂的算法和计算,那么您应该将它们实现为控制器。

希望这对您有帮助:)

That depends entirely on what they do. If they are merely views then you should just be adding views to the view hierarchy. E.g. if they are just showing some sort of info, then all you need is a view. If you do it this way, I would still recommend that you use a custom UIView subclass that handles your yellow views inside them and the layout.

If however, you find that each of them need to be doing quite a bit of "controller logic" e.g. handling complex algorithms and calculations then you should implement them as controllers.

Hope this is of help to you :)

分开我的手 2025-01-05 18:03:27

嗯,这两种都是可能的解决方案,各有利弊,正确的选择取决于应用程序的行为。

使用 BlueViewControllers 需要更多行代码,但我认为如果您想要不同的行为并且喜欢组织良好的方法,那么这是最好的解决方案。

另一方面,将 YellowViews 直接添加到 rootViewController 可能会更容易,特别是如果您的应用程序没有很多视图和复杂的架构。

一般来说,我更喜欢第一种方法,因为即使您的项目在开发时不需要 BlueViewController,它也应该有助于将来的更新。

Well, both are possible solutions, with pros and cons, and the right choice depends on the behavior of your app.

Using BlueViewControllers requires more lines code, but I think it is the best solution if you want different behaviors and you like well organized approach.

On the other side, adding YellowViews directly to the rootViewController could be easier, especially if your app doesn't have many views and an elaborate architecture.

In general I prefer the first approach, because, even if your project doesn't require BlueViewControllers at the moment of its development, it should help with future updates.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文