为什么将视图从一个视图控制器插入另一个视图控制器是不正确的?

发布于 2024-12-28 08:47:54 字数 601 浏览 3 评论 0原文

在 iPad 应用程序中,我有一个包含多个视图控制器的选项卡控制器。其中一个视图控制器(称为 MainViewController)需要 2 个并排的表视图。

因此,我编写了 2 个 UITableViewController 子类,并从 MainViewController 中分配/初始化了 UITableViewController 的两个子类,并将每个子类的表视图添加到 MainViewController 的视图中。

这意味着 UITableViewController 子类的视图是 MainViewController 视图的子视图。

这个答案: https://stackoverflow.com/a/7684648/191463 说这样做是不正确的,而且苹果似乎正在开始减少它。

我真的不想将两个 UITableViewController 中的所有代码都放在 MainViewController 中,因为如果我想在应用程序中的其他地方单独使用其中一个表视图,那么这将使阅读变得更加困难,并且将来可能会创建重复的代码。

这实际上是一个问题,如果是我该如何正确地做到这一点?

In an iPad application I have a tab Controller containing several view controllers. One of these view Controller (call it MainViewController) needs 2 table views side by side.

So I wrote 2 UITableViewController subclasses and from MainViewController, I alloc/init both subclasses of UITableViewController, and add the tableview from each to the MainViewController's view.

This means that UITableViewController subclasses's views are subviews of MainViewController's view.

This answer: https://stackoverflow.com/a/7684648/191463 says that doing that is incorrect and it seems Apple are starting to cut down on it.

I really do not want to have to put all the code from both UITableViewControllers in MainViewController, as it will make it much harder to read and in future could create duplicate code, if I want to use one of the tableview elsewhere in the app by itself.

Is this actually a problem, if it is how do I do it properly?

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

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

发布评论

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

评论(3

︶ ̄淡然 2025-01-04 08:47:54

苹果并没有削减它。这是在 iOS 5 之前创建自定义容器视图控制器的唯一方法。Apple 实际上听取了开发人员的意见,并让在 iOS 5 中使用子视图控制器和父视图控制器方法更容易完成此类操作,更不用说他们做到了它是分层运作的。

在大多数情况下,就应用程序崩溃或性能或任何其他方面而言,这实际上不会成为问题。在某些情况下这可能是一个问题,因为假设您有一个子视图控制器。您将视图控制器的视图添加到根视图控制器。在 iOS 5 之前,子视图控制器包括导航控制器视图控制器、选项卡栏控制器视图控制器和模态视图控制器。当您有一个调用 [self.parentViewController DismissModalViewControllerAnimated:YES]; 的按钮时会发生什么?从技术上讲,视图控制器不会呈现为模式视图控制器,您将视图添加到根视图控制器视图中。

在 iOS5 中,您可以向视图控制器添加子视图控制器,并且可以从一个子视图控制器转换到另一个子视图控制器。

现在,即使您的视图控制器没有不同的父视图控制器,将“根”视图控制器添加到另一个根视图控制器也不是最好的方法(特别是因为您无法访问父视图控制器,除非您在子视图控制器中显式创建一个parentViewController指针)。所以最终,苹果只是让它变得更容易、更解耦。

Apple isn't cutting down on it. This is the only way to create custom container view controllers prior to iOS 5. Apple actually listened to the developers and made it easier to do this sort of thing in iOS 5 with child and parent view controller methods, not to mention they made it so it worked hierarchically.

In most cases, this wouldn't actually be a problem in terms of applications crashing or performance or anything. It can be a problem in some cases, because let's say you have a child view controller. You add the view controller's view to your root view controller. Prior to iOS 5, child view controllers were things like navigation controller view controllers, tab bar controller view controllers, and modal view controllers. What happens when you have a button that calls [self.parentViewController dismissModalViewControllerAnimated:YES];? Technically, the view controller isn't being presented as a modal view controller, you added the view to the root view controller view.

In iOS5, you're able to add child view controllers to view controllers and are able to transition from one child view controller to another.

Now even if your view controller doesn't have a different parent, adding a "root" view controller to another root view controller isn't the best way to do it (especially since you don't get access to the parent view controller unless you explicitly create a parentViewController pointer in the child view controller). So in the end, Apple just made it easier and more decoupled.

[浮城] 2025-01-04 08:47:54

只要您负责管理 viewController 生命周期事件,就可以这样做。

initWithNibName...
loadView:
viewDidLoad:...
.
.
viewDidUnload..
dealloc
memoryWarnings
orientation changes

因此,如果您创建自定义“容器视图控制器”,您就有责任在适当的时间调用子 viewController 上的所有这些方法。把它想象成“如果你要实现 UITabBarController”,你需要管理哪些子视图??”

它很快就会变得复杂。添加另一个 viewController 的视图作为子视图是小菜一碟。iOS

5 通过以下方式为你做了一些这样的事情:指定父子关系,但是我仍然没有在任何地方看到任何示例代码。

It is OK to do it so long you take the responsibility of managing the viewController life cycle events

initWithNibName...
loadView:
viewDidLoad:...
.
.
viewDidUnload..
dealloc
memoryWarnings
orientation changes

So if you create a custom "container view controller" it becomes your responsibility to call all these methods on child viewControllers at the appropriate time. Think of it as "If you were to implement UITabBarController" what all will you have to manage regarding the children ??"

It quickly gets complex. Adding another viewController's view as subview is childs play.

iOS 5 does some of this stuff for you by specifying parent child relationship, however I still haven't seen any sample code anywhere yet to point to.

青朷 2025-01-04 08:47:54

我想说创建视图控制器容器并不是不正确或错误的,尤其是当苹果工程师自己这样做时。 UITabBarController、UINavigationController 或 UISplitViewController - 它们都是视图控制器容器。更重要的是,许多具有独特用户体验的优秀应用程序比您想象的更常见。然而,真正的问题是很难以正确的方式做到这一点,因此视图生命周期、内存管理和旋转处理是沿着视图层次结构正确完成的。幸运的是,Apple 人员做得不错,iOS5 引入了许多有关控制器容器的功能:

如果您有兴趣如何以上问题在iOS5之前必须得到解决,请阅读这两篇非常好的博文:

I'd say it is not incorrect or wrong to create view controller containers, especially when Apple engineers do that themselves. UITabBarController, UINavigationController or UISplitViewController - they are all view controller containers. More over many great apps with unique UX do that more common than you think. However the real issue is that it's quite hard to do it the right way, so e.g. view lifecycle, memory management and rotation handling is done properly along the hierarchy of views. Fortunately Apple guys did a decent job and iOS5 introduced lots of functionalities regarding controller containers:

If you're interested how above problems had to be addressed before iOS5, read these two very good blog posts:

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