PDA

View Full Version : unrecognized selector sent to instance




scriptnoob
Jul 25, 2008, 04:58 PM
Hi all,
I want to display list of files selected with NSOpenPanel in NSTableView with this code:


#import <Cocoa/Cocoa.h>


@interface AppController : NSObject {
IBOutlet NSTextField *tagTitle;
IBOutlet NSTableView *filesTableView;
NSArray *openedFiles;
}
- (IBAction)showOpenPanel:(id)sender;
- (void)openPanelDidEnd:(NSOpenPanel*)sheet
returnCode:(int)returnCode
contextInfo:(void*)contextInfo;
@end



#import "AppController.h"


@implementation AppController

- (IBAction)showOpenPanel:(id)sender
{
NSOpenPanel *panel = [NSOpenPanel openPanel];

[panel setCanChooseFiles: YES];
[panel setCanChooseDirectories: NO];
[panel setAllowsMultipleSelection: YES];
NSArray *fTypes = [NSArray arrayWithObjects: @"txt", nil];

[panel beginSheetForDirectory: nil
file: nil
types: fTypes
modalForWindow: [filesTableView window]
modalDelegate: self
didEndSelector: @selector(openPanelDidEnd:
returnCode:
contextInfo:)
contextInfo: nil];
}

- (void)openPanelDidEnd:(NSOpenPanel*)sheet
returnCode:(int)returnCode
contextInfo:(void*)contextInfo
{
if (returnCode == NSOKButton) {
openedFiles = [sheet filenames];
[filesTableView reloadData];
}
// NSRunAlertPanel(@"Test: ", @"Test", nil, nil, nil);
}

- (int)numberOfRowsInTableView:(NSTableView *)tv
{
return [openedFiles count];
}

- (id)tableView:(NSTableView *)tv
objectValueForTableColumn:(NSTableColumn *)tableColumn
row:(int)row
{
NSString *filename = [openedFiles objectAtIndex: 0];
return filename;
}

@end


but when I select files in NSOpenPanel and click Open I get this error:
[NSRectSet objectAtIndex:]: unrecognized selector sent to instance 0x1b9030

When line with
NSRunAlertPanel(@"Test: ", @"Test", nil, nil, nil);
is uncommented everything works fine.



Catfish_Man
Jul 25, 2008, 06:40 PM
openedFiles probably needs a retain.

scriptnoob
Jul 26, 2008, 10:37 AM
You're right. Thank you!