1

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!

1 Answer 1

1

You have two separate and unrelated instances of ValueItem *vi in your two classes. That explains why you set it up in the first class, but in the second vi is still nil.

You should be able to fix it by doing this:

rwc = [[ResultWindowController alloc] init];
[rwc setVi:self->vi];                // <--- this
[rwc setShouldCascadeWindows:NO];
[rwc showWindow:self];

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:

ValueItem *vi = [(AppDelegate*)[UIApplication sharedApplication].delegate vi];

when you need to access vi. You could then remove the vi ivar declared in RootWindowController (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:

ValueItem *vi = [MyModel sharedModel].vi;

far more readable and concise.

Sign up to request clarification or add additional context in comments.

5 Comments

Hello sergio, sorry I'm still new to all that stuff. I just thought with ValueItem *vi I just make the model accessible without creating a different one. How can I actually access the same instance of ValueItem or do I need some other strategy?
Awesome! Thanks a lot! It works but could you let me know why [rwc setVi:self->vi];' prevent creating a new and independent from ValueItem`?
glad to have helped. unfortunately, I can't understand what you are further asking...
Sorry my fault. I don't understand how I create the second instance of ValueItem in ResultWIndowsController? I don't do [... alloc]init]] again?
You would not do that in RootWindowController, rather in your model. Then your model should support multiple values of vi (an array?) if you add some more ode of what you are trying to do, it would help...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.