目标C:存储所有视图中可访问的变量 [英] Objective C: store variables accessible in all views
问题描述
问候,
我想写我的第一个iPhone应用程式。我需要能够访问所有视图中的数据。
I'm trying to write my first iPhone app. I have the need to be able to access data in all views. The data is stored when the user logs in and needs to be available to all views thereafter.
我想创建一个静态类,但是当我尝试使用时,访问静态类,我的应用程序崩溃,在控制台上没有输出。
I'd like to create a static class, however I when I try to access the static class, my application crashes with no output on the console.
是将数据写入文件的唯一方法吗?
Is the only way to write data to file? Or is there another cleaner solution that I haven't thought of?
提前非常感谢,
推荐答案
使用单例类,我一直使用它们的全局数据管理器类,需要从应用程序中的任何地方访问。您可以创建一个这样的简单的:
Use a singleton class, I use them all the time for global data manager classes that need to be accessible from anywhere inside the application. You can create a simple one like this:
@interface NewsArchiveManager : NetworkDataManager
{
}
+ (NewsArchiveManager *) sharedInstance;
@end
@implementation NewsArchiveManager
- (id) init
{
self = [super init];
if ( self )
{
// custom initialization goes here
}
return self;
}
+ (NewsArchiveManager *) sharedInstance
{
static NewsArchiveManager *g_instance = nil;
if ( g_instance == nil )
{
g_instance = [[self alloc] init];
}
return g_instance;
}
- (void) dealloc
{
[super dealloc];
}
@end
这篇关于目标C:存储所有视图中可访问的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!