0

我正在编写一个 OSX 应用程序,其中打开第二个窗口以在按下第一个窗口上的按钮时显示结果。窗口 2 开始正常并显示我想要的。但是,当我在 window-1 中更改输入并再次点击操作按钮时,window-2 不会更新结果。

这是我的问题:

  1. 在window-1中输入更改后window-2的内容如何更新
  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]; 

}

resultarrayarraycontrollerResultWindowController.xib 中的内容源。arraycontroller绑定到应该显示数组内容的表视图。这不是第二次更新。

4

1 回答 1

0

我相信您必须简单地执行以下操作:

- (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 setValueArray:[toDoItemArrayController arrangedObjects]];
        [rwc setNumberOfCalculations:[NSNumber numberWithInt:[_inputNumberOfCalculations intValue]]];
        [rwc calculateResults]; //starts method in 2nd-window controller for result 
    [rwc showWindow:self]; 
}

如果 ResultWindowController 不存在,您只是在更改它的值。无论如何,您都想更改值,而不是启动新实例。所以,只要你不使用 [[alloc]init] 你就很好。

希望有帮助。如果您需要其他任何内容,请发表评论。

编辑

要为您的 tableView 创建属性,请执行以下操作:

在.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;
@property (assign) NSTableView *yourTableView;   //Add this code
-(void)calculateResults;

@end

这里是 ResultWindowController.m

#import "ResultWindowController.h"
#import "ResultItem.h" //Your result model

@implementation ResultWindowController
@synthesize valueArray, resultArray, numberOfCalculations;
@synthesize yourTableView;  //Add this code

然后你应该可以打电话[yourTableView reloadData]

于 2013-01-15T21:22:05.193 回答