這是我最後一個的後續問題: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