I’m writing an OSX app where a second window is opened to show results when the button on the first window is pushed. The window-2 start fine and shows what I want. But when I change inputs in window-1 and hit the action button again the window-2 doesn’t update the results.
here my questions:
- how does the content of window-2 update after input change in window-1
- how is window-2 closed and released (right now window-2 shows up with the same content before closed when action button is pushed again)
here is the code for the action button:
- (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];
}
It might be easy but I’m afraid to always create an other ResultWindowController instance.
Thanks in advance.
Joerg
here is the 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
and here the 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 is the content source for an arraycontroller in the ResultWindowController.xib. The arraycontroller is bound to a table view which is supposed to show the array content. This is not updated the second time.
I believe you must simply do the following:
You were only changing values of ResultWindowController if it did not exist. You want to change values no matter what, just not start a new instance. So, as long as you don’t use [[alloc]init] you’re good.
Hope that helps. If you need anything else, drop a comment.
Edit
To create a property for your tableView do the following:
In .h
and here the ResultWindowController.m
Then you should be able to call
[yourTableView reloadData]