Domanda

I'm the developer of an in-house program for modeling harmonics and other properties of liquid-filled shells. Right now, the whole program assumes that there's only one set of physical properties (temperature, pressure, etc.) being used at a time in calculations. I have already broken all of the calculations out into a Sphere model. I have a controller which owns a sphere. When the user changes the physical properties the controller has the sphere recalculate everything and updates all the windows which are displaying the results of sphere calculations.

Now someone has asked me to make a table displaying the frequencies of a specific harmonic over a range of temperatures and pressures. I think this is going to require a new controller that has its own sphere model because it needs to be independent of all the other windows.

Here's my question: Should my new controller be an NSWindowController subclass or should it be an NSObject subclass with a property that is an NSWindow whose text fields are bound to values in the controller (or something completely different)? I'm the only developer in the company and I learned Cocoa on my own as I wrote this program over the past four years so I'm not sure I've always followed best practices. Since I'm about to introduce a new significant functionality I'd like to make sure I'm doing it right.

Not sure this matters, but the solution has to run under OS X 10.5 because we still have some G5 machines in the organization.

È stato utile?

Soluzione

If you have a controller that controls a particular window, then you should definitely use an NSWindowController subclass, if only because NSWindowController handles some of the more complex nib-loading and memory management issues for you.

Unless the Sphere model in your new window will be showing a different set of data than the one in your main controller, you don't need to create a new model for your new controller. You can just reference the sphere instance in your main controller.

Something like this:

.h:

#import <Cocoa/Cocoa.h>

@class Sphere;

@interface FrequenciesController : NSWindowController
{
    Sphere* sphere;
}
- (id)initWithSphere:(Sphere*)aSphere;   
@end

.m:

#import "FrequenciesController.h"
#import "Sphere.h"

@implementation FrequenciesController
- (id)initWithSphere:(Sphere*)aSphere
{
    self = [super initWithWindowNibName:@"NameOfYourNib"];
    if (self) 
    {
        sphere = [aSphere retain];
    }
    return self;
}

- (void)dealloc
{
    [sphere release];
    [super dealloc];
}
@end

To create the window, you then just have to do something like this in your main controller, assuming that you have declared frequenciesController as an ivar:

- (IBAction)showFrequenciesWindow:(id)sender
{
    if(!frequenciesController)
    {
        frequenciesController = [[FrequenciesController alloc] initWithSphere:self.sphere];
        [frequenciesController showWindow:self];
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top