将视图控制器添加到视图层次结构的正确方法是什么? [英] What's the proper way to add a view controller to the view hierarchy?

查看:89
本文介绍了将视图控制器添加到视图层次结构的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个视图控制器(A),它加载一个视图控制器(B)并在我的视图层次结构中使用它的视图。如果我将B的视图添加到A的视图层次结构中,并且我不手动转发像viewWillAppear这样的事件,我无法在B控制器中处理它们。 (来自viewWillAppear:docs)

I have a view controller (A) that loads a view controller (B) and uses it's view in my view hierarchy. If I add B's view to A's view hierarchy, and I don't manually forward events like viewWillAppear, I can't handle them in the B controller. (From the viewWillAppear: docs)


警告:如果将属于
视图控制器的视图添加到视图$ b直接$ b层次结构,视图
控制器将不会收到此
消息。如果您向视图层次结构插入或添加视图
,并且它具有
视图控制器,则应直接向
关联视图控制器发送此
消息。未能发送
视图控制器此消息将
阻止显示
的任何关联动画。

Warning: If the view belonging to a view controller is added to a view hierarchy directly, the view controller will not receive this message. If you insert or add a view to the view hierarchy, and it has a view controller, you should send the associated view controller this message directly. Failing to send the view controller this message will prevent any associated animation from being displayed.

嵌套视图控制器的正确方法是什么? (就像NavBarController那样。)如果只是需要将一组事件转发到嵌套控制器的问题,我需要转发的所有事件是什么?

What's the correct way to nest view controllers? (Like NavBarController does it.) If it's just a question of needing to forward a group of events to the nested controller, what are all of the events that I need to forward?

推荐答案

这里没有神奇的解决方案。正确的解决方案是手动发送这些消息。

There is no magic solution here. The correct solution is to manually send these messages.

viewWillAppear:/ viewDidAppear:和viewWillDisappear:/ viewDidDisappear:消息是您手动发送给孩子所需的唯一消息查看控制器。

The viewWillAppear:/viewDidAppear: and viewWillDisappear:/viewDidDisappear: messages are the only messages you need to manually send to the child view controller.

您应该在父视图控制器中实现所有这四种方法,并在父级接收消息和子级时将相同的消息发送到子视图控制器已加载。

You should implement all four of these methods in the parent view controller and send the same message to the child view controller whenever the parent receives the message and the child is loaded.

此外,当您添加子视图控制器的视图时,如果父视图的视图是非零的,则应发送viewWillAppear:/ viewDidAppear:消息。删除视图时,如果父视图的view.window为非零,则应发送viewWillDisappear:/ viewDidDisappear:消息。

In addition, when you add the child view controller's view, you should send the viewWillAppear:/viewDidAppear: messages if the parent's view.window is non-nil. When you remove the view, you should send the viewWillDisappear:/viewDidDisappear: messages if the parent's view.window is non-nil.

这篇关于将视图控制器添加到视图层次结构的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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