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

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

问题描述

我刚刚观看了关于实施UIViewController遏制的2011 WWDC 演示文稿(这里是视频的链接

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

他们提到了将viewControllers添加到屏幕,我希望对最佳做法做一些澄清...

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:animated:/ popViewControllerAnimated

他们在演示文稿中真的快速浏览了这个

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

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

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.

当使用视图控制器包含时,您基本上必须像往常一样将视图添加到包含视图(即使我必须这样做f添加控制器)。然后使用 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 vs pushViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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