How to call array from the model-class in my NSWindowController? The valueArray is set in AppDelegate, to the model-class ValueItem:
@interface AppDelegate : NSObject <NSApplicationDelegate>
{
ValueItem *vi;
ResultWindowController *rwc;
IBOutlet NSArrayController *outArrayController;
}
and
@implementation AppDelegate
....
- (IBAction)pushOk:(NSButton *)sender
{
self->vi = [[ValueItem alloc]init];
[vi setValueArray:[outArrayController arrangedObjects]];
NSLog(@"vi.valueArray is:%@", vi.valueArray);
if (rwc)
{
[rwc close];
}
rwc = [[ResultWindowController alloc] init];
[rwc setShouldCascadeWindows:NO];
[rwc showWindow:self];
}
Calling NSLog(@"vi.valueArray is:%@", vi.valueArray); return the arrays content just fine. But when I try to use it in my other NSWindowController it return always NULL:
@interface ResultWindowController : NSWindowController
{
ValueItem *vi;
NSNumber *resultAverage;
}
and
@implementation ResultWindowController
@synthesize resultAverage;
...
- (IBAction)pushChange:(NSButton *)sender
{
[self calculateAverage];
[_outputLabel setDoubleValue:[resultAverage doubleValue]];
NSLog(@"resultAverage is:%@", resultAverage);
NSLog(@"vi.valueArray is:%@", vi.valueArray);
}
-(void)calculateAverage
{
resultAverage = [vi.valueArray valueForKeyPath:@"@avg.nomValue"];
}
I can’t find the missing link? What do I miss here?
Thanks!
You have two separate and unrelated instances of
ValueItem *viin your two classes. That explains why you set it up in the first class, but in the secondviis stillnil.You should be able to fix it by doing this:
In order to do that, you should define a proper setter method in
RootWindowController.Alternatively, if you want to make your AppDelegate act as a model, you could do:
when you need to access
vi. You could then remove theviivar declared inRootWindowController(since you would access directly the one in you app delegate).Actually, it would be better creating a separate class acting as a model. It could be a singleton and you could access it like this:
far more readable and concise.