-1

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?

Community
  • 1
  • 1
Sebastian Flückiger
  • 5,525
  • 8
  • 33
  • 69

1 Answers1

1

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.

zoul
  • 102,279
  • 44
  • 260
  • 354
  • hi - thanks for the advice - i'll start from scratch, the storyboard seems to hide too much for me to actually see whats going on :) – Sebastian Flückiger Feb 26 '12 at 17:18
  • an im sorry for the -1 im on a very old phone and miscliked, ill fix it once im on my laptop again, somehow i cant switch the vote from mobile. – Sebastian Flückiger Feb 26 '12 at 17:19
  • i managed it =) i wrote an own tabbarcontroller, let the factory create all the views, and directly add the common datamodel to all of them before sending them to the tabbarcontroller. a very small question: why did you put the @class to some of the files in the sample project - i left that out and it works just fine? – Sebastian Flückiger Feb 26 '12 at 18:14
  • Great, congratulations. The factory should also create the main tab bar controller, is that how you did it? The `@class` declaration is a way to let the compiler know that some identifier is a class. You can do the same by importing the relevant header, but then the file gets recompiled each time the header changes. Just a small habit, nothing important. – zoul Feb 26 '12 at 18:33
  • yep all in the factory. ive got a function `buildInterface` there, that returns a tabbarcontroller loaded with 2 viewcontrollers, that both have the factorys datamodel as member =) called like that: `[window setRootViewController:[self buildInterface]];` in your buildMainFunction that i used unchanged. thanks a lot! – Sebastian Flückiger Feb 26 '12 at 18:46