当WindowDidLoad完成时,NSMutableArray重置自身

时间:2013-12-26 04:27:08

标签: objective-c macos cocoa nsmutablearray

当我使用@property@synthesize将NSMutableArray从控制器类传递到NSWindowController类时,我能够在windowDidLoad方法中使用数组的对象。 但是,在方法完成后,我单击窗口上的按钮触发IBAction,传递的值为nil

任何人都可以解释为什么会发生这种情况以及如何保存NSMutableArray吗?

以下是代码:

passClass.h

#import <Foundation/Foundation.h>
@class ResultWindowController;
@interface passClass : NSObject {
@private
    IBOutlet NSTextField *searchField;
    ResultWindowController *resultWindowController;

}
- (IBAction)passIt:(id)sender;
@end

passClass.m

#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

ResultWindowController.h

#import <Cocoa/Cocoa.h>

@interface ResultWindowController : NSWindowController <NSTableViewDataSource> {
    IBOutlet NSTableView *resultView;
    NSMutableArray *resultList;
    //NSMutableArray *array;

}
- (IBAction)returnValue:(id)sender;
@property (nonatomic,strong) NSMutableArray *array;
@end

ResultWindowController.m

#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< [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

这是NSLog的结果:

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)

2 个答案:

答案 0 :(得分:0)

尝试从ResultWindowController类声明中删除NSMutableArray *array;,并仅为其保留属性声明。 或者您可以尝试在ResultVWindowsContorller init类中启动数组属性,并在- (IBAction)passIt:(id)sender中添加对象到数组。

答案 1 :(得分:0)

老实说,除非在-windowDidLoadNib中你期望array为空,否则我根本无法看到它是如何运作的。

合成属性时the default name of the instance variable that is used is prefixed by an underscore。因此,代码中的类有两个实例变量array_array

有几种方法可以解决这个问题。以下是我认为你应该做的是删除接口定义中的实例变量。然后,每次使用它时,您都会开始每次都收到编译错误。通过使用属性来修复它们,例如,行

result.resultName = [array objectAtIndex:i];
-windowDidLoadNib中的

变为

result.resultName = [self.array objectAtIndex:i];