Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

this is a follow-up question to my last one here: iOS: Initialise object at start of application for all controllers to use .

I have set my application up as follows (ignore the DB Prefix):

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

i will go step by step through the application, and then post the problem in the end

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

the app builds fine, and starts up but the label stays blank. so either the object does not exist (but i guess this would result in an error message), or something else is wrong with my setup. any thoughts?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
140 views
Welcome To Ask or Share your Answers For Others

1 Answer

Two notes:

  1. Your have a shotgun approach to asking questions: everytime you hit a stumbling block, you ask a?question and if the answer does not work immediately, you ask another one. You have to spend some energy in between the questions debugging and poking into the code on your own, otherwise you will depend on the external help forever.

  2. Use the common coding style please. CAPS are reserved for macros.

Now to the code:

- (BOOL) …didFinishLaunching…
{
    DBFactoryClass *factory = [[DBFactoryClass alloc] init];
    return YES;
}

This simply creates an instance of the DBFactoryClass and then throws it away. In other words, it’s essentially a no-op. Judging by the comments in the previous answer you create the controllers using the Storyboard feature. How are they supposed to receive the reference to the data model? The reference isn’t going to show up by magic, you have to assign it somewhere.

I’m not familiar with the Storyboard feature. The way I would do it is to create the view controllers using separate XIB files, then you can create the controller instances in the Factory class and pass them the needed reference to the model. In the end the application delegate would create the factory, ask it to assemble the main controller and then set it as the root view controller for the window. Just like in my sample project. It’s possible that there’s a way to make it work with storyboards, but as I said, I am not familiar with them.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...