这是我最后一个的后续问题:iOS: Initialise object at start of application for all controllers to use。iOS上无法访问全局实例(由工厂构建)
我已经设置我的应用程序,如下所示(忽略DB前缀):
DBFactoryClass // Built a DataManaging Object for later use in the app
DBDataModel // Is created by the factory, holds all data & access methods
DBViewControllerA // Will show some of the data that DBDataModel holds
moreViewControllers that will need access to the same DBDataModel Object
我将通过应用走一步看一步,然后在最后
AppDelegate中发布问题。^h
#import "DBFactoryClass.h"
AppDelegate.m
- (BOOL)...didFinishLaunching...
{
DBFactoryClass *FACTORY = [[DBFactoryClass alloc ]init ];
return YES;
}
DBFactoryClass.h
#import <Foundation/Foundation.h>
#import "DBDataModel.h"
@interface DBFactoryClass : NSObject
@property (strong) DBDataModel *DATAMODEL;
@end
DBFactoryClass.m
#import "DBFactoryClass.h"
@implementation DBFactoryClass
@synthesize DATAMODEL;
-(id)init{
self = [super init];
[self setDATAMODEL:[[DBDataModel alloc]init ]];
return self;
}
@end
ViewControllerA.h
#import <UIKit/UIKit.h>
#import "DBDataModel.h"
@class DBDataModel;
@interface todayViewController : UIViewController
@property (strong)DBDataModel *DATAMODEL;
@property (weak, nonatomic) IBOutlet UILabel *testLabel;
@end
ViewControllerA.m
#import "todayViewController.h"
@implementation todayViewController
@synthesize testLabel;
@synthesize DATAMODEL;
- (void)viewDidLoad
{
todaySpentLabel.text = [[DATAMODEL test]stringValue]; // read testdata
}
@end
DBDataModel.h
#import <Foundation/Foundation.h>
@interface DBDataModel : NSObject
@property (nonatomic, retain) NSNumber* test;
@end
DBDataModel.m
#import "DBDataModel.h"
@implementation DBDataModel
@synthesize test;
-(id)init{
test = [[NSNumber alloc]initWithInt:4]; // only a testvalue
return self;
}
@end
应用程序建立的罚款,并开始了,但标签保持空白。所以要么这个对象不存在(但我想这会导致一个错误信息),或者其他的错误与我的设置。有什么想法吗?
嗨 - 感谢您的建议 - 我会从头开始,故事板似乎隐藏太多,我实际上看到了什么事情:) – 2012-02-26 17:18:12
一个即时通讯对不起一个非常老的电话和错误,我再次在我的笔记本电脑上修复它,不知何故,我无法切换从移动的投票。 – 2012-02-26 17:19:32
我管理它=) 我写了一个自己的tabbarcontroller,让工厂创建所有的意见,并直接添加到他们所有的公共数据模型,然后将它们发送到tabbarcontroller。 一个非常小的问题:为什么你把@class放在示例项目中的一些文件中 - 我把它留在了外面,它工作得很好? – 2012-02-26 18:14:19