我正在编写一个 OSX 应用程序,其中打开第二个窗口以在按下第一个窗口上的按钮时显示结果。窗口 2 开始正常并显示我想要的。但是,当我在 window-1 中更改输入并再次点击操作按钮时,window-2 不会更新结果。
这是我的问题:
- 在window-1中输入更改后window-2的内容如何更新
- window-2 是如何关闭和释放的(现在再次按下操作按钮时,window-2 在关闭之前显示相同的内容)
这是操作按钮的代码:
- (IBAction)pushRun:(id)sender {
if (!rwc)
{
rwc = [[ResultWindowController alloc] init];
[rwc setValueArray:[toDoItemArrayController arrangedObjects]];
[rwc setNumberOfCalculations:[NSNumber numberWithInt:[_inputNumberOfCalculations intValue]]];
[rwc calculateResults]; //starts method in 2nd-window controller for result calculation
}
[rwc showWindow:self];
}
这可能很容易,但我害怕总是创建另一个 ResultWindowController 实例。
提前致谢。约尔格
这是 ResultWindowController.h:
#import <Cocoa/Cocoa.h>
@interface ResultWindowController : NSWindowController{
NSArray *valueArray;
NSMutableArray *resultArray;
NSNumber *numberOfCalculations;
}
@property (nonatomic, retain, readwrite) NSArray *valueArray;
@property (retain) NSNumber *numberOfCalculations;
@property (nonatomic, retain, readwrite) NSMutableArray *resultArray;
-(void)calculateResults;
@end
这里是 ResultWindowController.m
#import "ResultWindowController.h"
#import "ResultItem.h" //my result model
@implementation ResultWindowController
@synthesize valueArray, resultArray, numberOfCalculations;
- (id)init
{
if(![super initWithWindowNibName:@"ResultWindow"])
return nil;
return self;
}
-(void)awakeFromNib
{
}
- (void)windowDidLoad
{
[super windowDidLoad];
}
- (void)calculateResults
{
//a lot of calculation code ...
ResultItem *newResult = [[ResultItem alloc]init];
[newResult setValue:[nameArray objectAtIndex:i] forKey:@"name"];
[newResult setValue:[NSNumber numberWithDouble:avg] forKey:@"averageValue"];
[newResult setValue:[NSNumber numberWithDouble:min] forKey:@"minValue"];
[newResult setValue:[NSNumber numberWithDouble:max] forKey:@"maxValue"];
[newResult setValue:dimensionRandomArray forKey:@"randomArray"];
[resultArray addObject:newResult];
}
resultarray
是arraycontroller
ResultWindowController.xib 中的内容源。arraycontroller
绑定到应该显示数组内容的表视图。这不是第二次更新。