我正在编写一个OSX应用程序,其中第二个窗口打开,以便在按下第一个窗口上的按钮时显示结果。窗口2开始很好,并显示我想要的。但是当我在window-1中更改输入并再次点击操作按钮时,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
是ResultWindowController.xib中arraycontroller
的内容源。 arraycontroller
绑定到一个表视图,该视图应该显示数组内容。这不是第二次更新。
答案 0 :(得分: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]