何时使用 addChildViewController 与 PushViewController

发布于 2024-12-15 02:59:50 字数 974 浏览 4 评论 0原文

我刚刚观看了 2011 年 WWDC 演示,内容为“实现 UIViewController Containment”(这是视频的链接

他们提到了将 viewController 添加到屏幕的两种方法,我希望能对最佳实践进行一些澄清......

addChildViewController/removeFromParentViewController
与@property(非原子,只读)NSArray *childViewControllers 和[selftransitionFromViewController:currentView toViewController:nextView period: options:animations:completion:];

pushViewController:animated:/ 一起使用popViewControllerAnimated
他们在演示文稿中很快就跳过了这一点

在我的应用程序中,我使用所有自定义视图控制器,直到今天我一直用以下方式管理它们:

[nextController performSelector:@selector(setDelegate:) withObject:self];
[currentPageController.view removeFromSuperview];
[self.view addSubview:nextController.view];

但我现在明白这是不好的做法,我想知道什么使用“addChildViewController”的正确方法是什么?使用“pushViewController”的正确方法是什么?

我非常感谢您对此事的想法!

I just watched a 2011 WWDC presentation on "Implementing UIViewController Containment" (here's a link to the video)

They mentioned both of these ways of adding viewControllers to the screen, and I would appreciate some clarification on best practices...

addChildViewController / removeFromParentViewController

used with an @property (nonatomic, readonly) NSArray *childViewControllers and [self transitionFromViewController:currentView toViewController:nextView duration: options: animations: completion:];

pushViewController: animated: / popViewControllerAnimated

they really quickly skimmed past this in the presentation

In my apps I use all custom viewControllers, and until today I have always managed them with:

[nextController performSelector:@selector(setDelegate:) withObject:self];
[currentPageController.view removeFromSuperview];
[self.view addSubview:nextController.view];

But I understand now that this is bad practice, and I'm wondering what is the correct way to use "addChildViewController" and what is the correct way to use "pushViewController"?

I really appreciate your thoughts on the matter!

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

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

发布评论

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

评论(1

驱逐舰岛风号 2024-12-22 02:59:50

是的,pushViewController:用于管理一堆视图控制器的导航控制器。 addChildViewController: 另一方面是 iOS 5 称为“视图控制器包含”功能的一部分。

这背后的基本思想是,您可以将视图控制器嵌入到您自己的其他视图控制器中(例如,当将 iPhone 应用程序移植到 iPad 时),从而轻松地实现您自己的导航控制器、分割视图控制器等功能

。像您展示的那样的实现的问题是您只处理视图。视图控制器事件(例如方向更改)将无法正确地沿层次结构传递。视图控制器包含尝试确保所有包含的视图控制器也将获得适当的消息。

查看您的实现,您还应该考虑您真正想通过此实现什么。导航控制器可能是正确的,或者您甚至可能以模态方式显示下一个控制器(请参阅 https://developer.apple.com/library/content/featuredarticles/ViewControllerPGforiPhoneOS/PresentingaViewController.html)使用这些方法时的奖励(例如导航控制器和模态视图)是用户已经熟悉这些导航技术。

无论如何 https://developer.apple.com/library/content/featuredarticles/ ViewControllerPGforiPhoneOS/ 是一本关于如何在视图控制器之间转换的好读物。

当使用视图控制器包含时,您基本上必须像往常一样将视图添加到包含视图中(即使添加了控制器也必须这样做)。然后使用 addChildViewController: 将子视图控制器添加到周围的视图控制器中。您还必须通过 didMoveToParentViewController: 通知子控制器它已被放入另一个控制器中。您还可以使用 transitionFromViewController:toViewController: 将一个视图控制器交换为另一个视图控制器,并可选择提供动画。

Yes, pushViewController: is for navigation controllers that manage a stack of view controllers. addChildViewController: on the other hand is part of an iOS 5 feature called "view controller containment".

The basic idea behind this is that you can embed your view controllers into other view controllers of your own (e.g. when porting an iPhone app to the iPad) and thus easily do your own implementation of things like navigation controllers, split view controllers etc.

One problem with an implementation like the one you show is that you only handle views. View controller events like orientation changes will not be passed properly down the hierarchy. View controller containment tries to ensure that all contained view controllers will get the appropriate messages, too.

Looking at your implementation you should also think about what you really want to achieve by this. A navigation controller may be the right thing or you might even show the next controller modally (see https://developer.apple.com/library/content/featuredarticles/ViewControllerPGforiPhoneOS/PresentingaViewController.html) A bonus when using these methods (e.g. navigation controllers and modal views) is that the user is already familiar with those navigation techniques.

In any case https://developer.apple.com/library/content/featuredarticles/ViewControllerPGforiPhoneOS/ is a good read about how to transition between view controllers.

When using view controller containment you basically have to add the view to the containing view as usual (this has to be done even if the controller is added). Then you use addChildViewController: to add the child view controller to the surrounding one. You also have to notify the child controller by didMoveToParentViewController: that it has been put into another controller. You can also use transitionFromViewController:toViewController: to exchange one view controller for another, optionally giving an animation.

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