Note that there are some explanatory texts on larger screens.

plurals
  1. PONSMutableArray resetting itself when WindowDidLoad is done
    primarykey
    data
    text
    <p>When I pass a NSMutableArray from a controller class to a NSWindowController class using <code>@property</code> and <code>@synthesize</code> I am able to use the objects of the array in the <code>windowDidLoad</code> method. However, after the method is done and I click a button on the window triggerig an <code>IBAction</code>, the passed value is <code>nil</code>.</p> <p>Can anyone explain me why this is happening and how I can preserve the NSMutableArray?</p> <p>Here is the code:</p> <p><strong>passClass.h</strong></p> <pre><code>#import &lt;Foundation/Foundation.h&gt; @class ResultWindowController; @interface passClass : NSObject { @private IBOutlet NSTextField *searchField; ResultWindowController *resultWindowController; } - (IBAction)passIt:(id)sender; @end </code></pre> <p><strong>passClass.m</strong></p> <pre><code>#import "passClass.h" #import "ResultWindowController.h" @implementation passClass - (IBAction)passIt:(id)sender { NSString *searchString = searchField.stringValue; NSMutableArray array = [[NSMutableArray alloc]init]; [array addObject:searchString]; [array addObject:searchString]; if(!resultWindowController) { resultWindowController = [[ResultWindowController alloc] initWithWindowNibName:@"ResultWindow"]; resultWindowController.array =[[NSMutableArray alloc]initWithArray:array copyItems:YES]; [resultWindowController showWindow:self]; } } @end </code></pre> <p><strong>ResultWindowController.h</strong></p> <pre><code>#import &lt;Cocoa/Cocoa.h&gt; @interface ResultWindowController : NSWindowController &lt;NSTableViewDataSource&gt; { IBOutlet NSTableView *resultView; NSMutableArray *resultList; //NSMutableArray *array; } - (IBAction)returnValue:(id)sender; @property (nonatomic,strong) NSMutableArray *array; @end </code></pre> <p><strong>ResultWindowController.m</strong></p> <pre><code>#import "Results.h" @interface ResultWindowController () @end @implementation ResultWindowController //@synthesize array; - (id)initWithWindow:(NSWindow *)window { self = [super initWithWindow:window]; if (self) { // Initialization code here. resultList = [[NSMutableArray alloc] init]; } return self; } - (void)windowDidLoad { [super windowDidLoad]; for (NSInteger i = 0; i&lt; [array count];i++) { Results *result = [[Results alloc]init]; result.resultName = [self.array objectAtIndex:i]; [resultList addObject:result]; [resultView reloadData]; NSLog (@"self.array: %@", self.array); // works fine, tableview gets populated, array is correct } } - (NSInteger) numberOfRowsInTableView:(NSTableView *)resultView{ return [resultList count]; } - (id)tableView:(NSTableView *)resultView objectValueForTableColumn:(NSTableColumn *)resultColumn row:(NSInteger)row{ Results *result = [resultList objectAtIndex:row]; NSString *identifier = [resultColumn identifier]; return [result valueForKey:identifier]; } - (IBAction)selectedSeries:(id)sender { NSLog (@"self.array: %@", self.array); //when I break here the array is nil } @end </code></pre> <p>Here is the NSLog result:</p> <pre><code>2013-12-26 10:36:49.487 MyProgram[545:303] self.array: ( "test", "test" ) 2013-12-26 10:37:24.044 MyProgram[545:303] self.array: (null) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload