1

如何从我的模型类中调用数组NSWindowController?在valueArrayAppDelegate 中设置为模型类ValueItem

@interface AppDelegate : NSObject <NSApplicationDelegate>
{
    ValueItem *vi;
    ResultWindowController *rwc;
    IBOutlet NSArrayController *outArrayController;
}

@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];

}

调用NSLog(@"vi.valueArray is:%@", vi.valueArray);返回数组内容就好了。但是当我尝试在我的另一个中使用它时,NSWindowController它总是返回NULL

@interface ResultWindowController : NSWindowController
{
    ValueItem *vi;
    NSNumber *resultAverage;
}

@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"];
}

我找不到丢失的链接?我在这里想念什么?谢谢!

4

1 回答 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.

于 2013-02-04T22:13:36.950 回答