视图控制器之间的通信 [英] Communication between view controllers

查看:95
本文介绍了视图控制器之间的通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于具有UINavigationController和2级UITableViews的应用程序部分(即,在根控制器上选择了一行,将第二个控制器推入导航堆栈),我有以下问题:

Given the section of an application which has a UINavigationController and 2 levels of UITableViews (i.e. a row is selected on the root controller which pushes the second controller onto the navigation stack) I have the following questions:

1)两个控制器都需要一个User对象。两个控制器之间进行通讯的最佳方式是什么?我在该站点上看到过一篇帖子,提到依赖注入,并且根控制器可以通过以下方式将User对象传递给第二级控制器:

1) There is a User object which is required by both controllers. What is the best way to communicate between the two controllers? I have seen a post on this site which mentions dependancy injection and that the root controller could pass the User object to the second level controller by:

@implementation SecondLevelViewController

-(void) initWithUser: (User *) user {
   // myUser is an instance variable
   myUser = user;
   [myUser retain];
}

在该示例中,第二个控制器似乎保留了User,而我看到了其他资料(例如斯坦福iPhone开发课程)提倡在这种情况下(松散耦合)简单地分配用户,而不是保留用户。

In that example the second controller would seem to retain the User whereas I have seen other sources (e.g. the Stanford iPhone development course) which advocate the User being simply assigned, and not retained, in that situation (loose coupling).

我也看到了争论对于将在第二个控制器上使用分配的id对象(而不是上面保留的User实例变量)的一种委托形式。

I have also seen arguments for a form of delegation where an assigned id object would be used on the second controller (rather than a retained User instance variable as above).

如果有人可以为我澄清这个立场,因为我对这方面的看似矛盾的建议感到非常困惑。视图控制器之间进行通信的最佳方式是什么?

It would be great if someone could clarify this position for me as I am extremely confused by the seemingly conflicting advice in this area. What is the best way for communication between view controllers?

2)我的第二个问题也与控制器的结构有关。我已经看到了一些示例,其中根控制器(在上面的布置中)具有一系列实例化的第二级控制器。在专业应用程序中,这是正常现象吗?以这种方式执行操作是否会对内存产生重大影响(即无延迟加载)?我认为数组的优点是减少了第二级控制器的加载时间?

2) My second question also relates to the structuring of the controllers. I have seen examples where the root controller (in the arrangement above) has an array of instantiated second level controllers. Is this normal in a professional application or would there be a significant memory impact for doing things this way (i.e. no lazy loading)? I assume the advantage of the array is a reduction of loading times for the second level controllers?

感谢您的任何回应,因为我正在尝试正确地开发东西而不是黑客。

Thank you for any responses as I am trying to develop things properly rather than hack them together.

推荐答案

1)视图控制器之间的最佳通信方式是什么?

1) "What is the best way for communication between view controllers?"

没有一种最佳方法。您当然可以在初始化时将模型对象传递给视图控制器。在这种情况下,控制器通常应保留模型。如果您正在编写着重于可重用性的通用视图控制器类(例如UITableViewController),则与委托一起使用是一个不错的选择。

There is no one "best" way. You can certainly pass a model object to the view controller on initialization. In that case, the controller should generally retain the model. Going with a delegate is a good option if you are writing a general view controller class with a focus on reusability (such as UITableViewController).

2)我已经看到了示例根控制器(在上面的安排中)具有实例化的第二级控制器数组。在专业应用程序中,这是否正常?

2) "I have seen examples where the root controller (in the arrangement above) has an array of instantiated second level controllers. Is this normal in a professional application"

首先,您应将帖子限制在所以每个帖子一个问题。视图控制器本身对内存的影响通常很小。它的实例变量只需要几百个字节。占用大量内存的部分是视图本身。而且,UIViewController的视图加载/卸载机制将在需要时小心地卸载占用大量内存的视图(即,如果它收到内存警告并且该视图当前不在屏幕上)。因此,只要您实现 viewDidLoad viewDidUnload didReceiveMemoryWarning 正确地,我不会太担心视图控制器数组的内存消耗。

Firstly, you should limit your posts on SO to one question per post. The memory impact of a view controller itself is usually quite low. It needs just a few hundred bytes for its instance variables. The memory-intensive part are the views themselves. And UIViewController's view loading/unloading mechanism will take care to unload the memory-intensive view when needed (i.e. if it receives a memory warning and the view is currently not on screen). So as long as you implement viewDidLoad, viewDidUnload and didReceiveMemoryWarning correctly, I wouldn't worry too much about memory consumption of an array of view controllers.

UITabBarController也持有一个子控制器数组,因此这没什么错。但是,您应该避免的是一个控制器拥有整个子控制器层次结构(即不仅是第二层控制器,而且还包含第三层控制器等):与其说是因为内存问题,还不如避免不必要的耦合。

UITabBarController holds an array of subcontrollers, too, so there is nothing wrong with that. What you should avoid, though, is for one controller to hold an entire hierarchy of subcontrollers (i.e. not only second-level, but also third-level etc.): not so much because of memory concerns than to avoid unnecessary coupling.

这篇关于视图控制器之间的通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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