何时使用 addChildViewController 与 pushViewController [英] When to use addChildViewController vs pushViewController

查看:26
本文介绍了何时使用 addChildViewController 与 pushViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚观看了 2011 WWDC 关于实现 UIViewController Containment"的演示(这是视频的链接)

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
与@property (nonatomic, readonly) NSArray *childViewControllers 和 [self transitionFromViewController:currentView toViewController:nextView duration: options: animations: completion:];

pushViewController:动画:/popViewControllerAnimated
他们在演示中很快就跳过了这个

pushViewController: animated: / popViewControllerAnimated
they really quickly skimmed past this in the presentation

在我的应用中,我使用所有自定义 viewController,直到今天我一直使用以下方法管理它们:

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];

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

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!

推荐答案

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

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".

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

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.

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

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.

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

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

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

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.

这篇关于何时使用 addChildViewController 与 pushViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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