从视图控制器引用模型的方法 [英] ways to reference a model from a view controller

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

问题描述

到目前为止,我遇到的所有例子(包括斯坦福播客)通过声明它作为View Controller的属性并使用它来引用一个Model:

All the examples I've come across so far ( including Stanford podcasts ) reference a Model by declaring it as a property of a View Controller and using it from there:

#import "myClass.h"    // assume it carries a single NSString property

@interface
@property (nonatomic,strong) myClass *myobject;
@end

@implementation ViewController

-(void)viewDidLoad {
self.myObject = [[myClass alloc] init]
.
.   
.
-(void)someMethod{
displayLabel = self.myObject.myString; 

看起来像是更多的self.CV比MVC。

对我自己这个作品:

seems like that's more self.C-V than M-V-C.
After messin' about on my own this works:

#import "myClass.h"

@implementation ViewController {
MyClass *myObject;
}

-(void)viewDidLoad {
myObject = [[myClass alloc] init]
}
.
.
.
-(void)someMethod{
displayLabel = myObject.myString;

我的问题是;在使用第二个例子有什么危险吗?

my question is; is there any danger in using second example? or to ask differently, does it give the compiler an easier task of keeping MODEL separate from VIEW and CONTROLLER?

推荐答案

使用单例或者把模型作为控制器的属性也是一个生命的问题。我喜欢使用单身人士帮助类(从游戏中心获取数据或从核心数据获取数据)。单例对于您需要从应用程序中的不同位置访问的事情(您的单例的生命周期长于您的视图控制器的生命周期)非常有用。但是对于视图控制器相关模型,你当然可以使用它们作为视图控制器的属性。让我们假设你的应用程序中有十个视图控制器,每个视图控制器显示完全不同的内容,所以保持内存中所有可能的视图控制器的所有数据(在单例中)是绝对没有意义的,只是为了在用户想要查看任何的视图控制器。在这种情况下,从视图控制器实现中加载模型的数据并将其作为一个属性是不可耻的。这保证了当视图控制器的生命周期结束时数据被自动释放,并避免冲突。当您显示从服务器加载的数据时,在每次显示数据时都不需要刷新数据,以减少加载数据时产生的流量,因此在单例中保留数据会有意义。使用单例对于线程安全可能是危险的。当数据从后台线程变异,同时迭代数据源对象以刷新表视图的内容。单态也可导致应避免的tigh耦合。如果要保存对对象的弱引用,则使用实例变量而不是属性仍然是一个不错的选择,因为如果引用对象被自动释放,它将自动设置为nil。在这种情况下,弱属性将导致不良访问。

Using singletons or holding the model as a property of the controller is also a question of lifetime. I prefer using singletons for helper classes (getting data from game center or fetching data from core data). Singletons are useful for things you need to access from different places within your app (where lifetime of your singleton is longer than your view controller's lifetime). But for view controller dependent models you can of course use them as properties of the view controller. Let's say you have ten view controllers in your app each displaying completely different content it does absolutely not make sense to keep all data for all possible view controllers in memory (in a singleton) just to have data ready in case the user wants to see any of the view controllers. In this case it's no shame to load your model's data from within your view controller implementation and hold it as a property. This guarantees that data is auto released when the view controller's lifetime ends and avoids conflicts. Holding data in singleton would make sense when you display data loaded from a server that does not need to be refreshed everytime you present your data to reduce the amount of traffic generated by loading the data. Using singletons might be dangerous regarding thread safety e.g. when data is mutated from a background thread while iterating over your datasource object to refresh a table view's content. Singletons can also lead to tigh coupling which should be avoided. Using an instance variable instead of a property is still a good choice if you want to hold a weak reference to an object as it is automatically set to nil if the referenced object gets auto released. A weak property would in this case lead to bad access.

这篇关于从视图控制器引用模型的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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