我的新控制器的超类应该是什么?

发布于 2024-12-23 18:26:06 字数 480 浏览 3 评论 0原文

我是一个内部程序的开发人员,用于对充液壳体的谐波和其他属性进行建模。目前,整个程序假设计算中一次仅使用一组物理属性(温度、压力等)。我已经将所有计算分解为球体模型。我有一个拥有一个球体的控制器。当用户更改物理属性时,控制器让球体重新计算所有内容并更新所有显示球体计算结果的窗口。

现在有人要求我制作一个表格,显示特定谐波在一定温度和压力范围内的频率。我认为这将需要一个具有自己的球体模型的新控制器,因为它需要独立于所有其他窗口。

这是我的问题:我的新控制器应该是 NSWindowController 子类还是应该是 NSObject 子类,其属性是 NSWindow,其文本字段绑定到控制器中的值(或完全不同的东西)?我是公司中唯一的开发人员,在过去的四年里,我在编写这个程序时自学了 Cocoa,所以我不确定我是否始终遵循最佳实践。由于我即将推出一项新的重要功能,因此我想确保我做得正确。

不确定这是否重要,但该解决方案必须在 OS X 10.5 下运行,因为我们组织中仍然有一些 G5 机器。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笙痞 2024-12-30 18:26:06

如果您有一个控制特定窗口的控制器,那么您绝对应该使用 NSWindowController 子类,因为 NSWindowController 处理一些更复杂的笔尖加载和内存管理为您解决的问题。

除非新窗口中的 Sphere 模型将显示与主控制器中不同的数据集,否则您不需要为新控制器创建新模型。您只需在主控制器中引用球体实例即可。

像这样的东西:

.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

要创建窗口,您只需在主控制器中执行类似的操作,假设您已将 frequenciesController 声明为 ivar:

- (IBAction)showFrequenciesWindow:(id)sender
{
    if(!frequenciesController)
    {
        frequenciesController = [[FrequenciesController alloc] initWithSphere:self.sphere];
        [frequenciesController showWindow:self];
    }
}

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];
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文