I am doing a challenge from Aaron Hillegass's book.
The application is supposed to have an NSTableView(with 2 columns) and two Buttons. The two buttons will be 'Add' and 'Delete'. When you press these buttons they will add an 'Employee' to the NSTableView, and Delete will remove a selected Employee.
When I click on my buttons nothing happens. In the example you are not supposed to use an NSArrayController or the bindings mechanism.
I suspect that I have missed doing something in Interface Builder. He mentions "In MyDocument.xib, you will set the identifier of each column to be the name of the variable you would like displayed."
Here is my code:
Person.h
Person.m
MyDocument.h
MyDocument.m
While highlighting the first Table Column, in the Attributes tab I have Identifier set to variable personName. In the second Table column attributes tab I have Identifier set to expectedRaise.
Do I need to set the Class Identity of the NSTableView to 'Person'? It won't let me so I'm guessing no.
Do I need to Control-drag from the MyDocument object to the NSTableView? I did that and no luck.
What am I missing to make this needed connection?
Thank you.
The application is supposed to have an NSTableView(with 2 columns) and two Buttons. The two buttons will be 'Add' and 'Delete'. When you press these buttons they will add an 'Employee' to the NSTableView, and Delete will remove a selected Employee.
When I click on my buttons nothing happens. In the example you are not supposed to use an NSArrayController or the bindings mechanism.
I suspect that I have missed doing something in Interface Builder. He mentions "In MyDocument.xib, you will set the identifier of each column to be the name of the variable you would like displayed."
Here is my code:
Person.h
Code:
#import <Foundation/Foundation.h>
@interface Person : NSObject {
NSString *personName;
float expectedRaise;
}
@property (readwrite, copy) NSString *personName;
@property (readwrite) float expectedRaise;
@end
Person.m
Code:
@implementation Person
@synthesize personName;
@synthesize expectedRaise;
-(id)init
{
[super init];
expectedRaise = 5.0;
personName = @"New Person";
return self;
}
-(void)dealloc
{
[personName release];
[super dealloc];
}
-(void)setNilValueForKey:(NSString *)key
{
if ([key isEqual:@"expectedRaise"])
{
[self setExpectedRaise:0.0];
}
else
{
[super setNilValueForKey:key];
}
}
@end
MyDocument.h
Code:
#import <Cocoa/Cocoa.h>
@class Person;
@interface MyDocument : NSDocument
{
NSMutableArray *employees;
IBOutlet NSTableView *tableView;
}
-(IBAction)createEmployee:(id)sender;
-(IBAction)removeEmployee:(id)sender;
@end
MyDocument.m
Code:
#import "MyDocument.h"
@implementation MyDocument
- (id)init
{
self = [super init];
if (self)
{
employees = [[NSMutableArray alloc] init];
}
return self;
}
-(void)dealloc
{
[employees release];
[super dealloc];
}
-(IBAction)createEmployee:(id)sender
{
Person *newEmployee = [[Person alloc] init];
[employees addObject:newEmployee];
[newEmployee release];
[tableView reloadData];
}
-(IBAction)removeEmployee:(id)sender
{
NSIndexSet *rows = [tableView selectedRowIndexes];
if ([rows count] == 0)
{
NSBeep();
return;
}
[employees removeObjectsAtIndexes:rows];
[tableView reloadData];
}
-(int)numberOfRowsInTableView:(NSTableView *)aTableView
{
return [employees count];
}
-(id)tableView:(NSTableView *)aTableView
objectValueForTableColumn:(NSTableColumn *)aTableColumn
row:(int)rowIndex
{
NSString *identifier = [aTableColumn identifier];
Person *person = [employees objectAtIndex:rowIndex];
return [person valueForKey:identifier];
}
-(void)tableView:(NSTableView *)aTableView
setObjectValue:(id)anObject
forTableColumn:(NSTableColumn *)aTableColumn
row:(int)rowIndex
{
NSString *identifier = [aTableColumn identifier];
Person *person = [employees objectAtIndex:rowIndex];
[person setValue:anObject forKey:identifier];
}
- (NSString *)windowNibName
{
//System Generated code here that I haven't touched but removed from example for readability
}
- (void)windowControllerDidLoadNib:(NSWindowController *) aController
{
//System Generated code here that I haven't touched but removed from example for readability
}
- (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError
{
//System Generated code here that I haven't touched but removed from example for readability
}
- (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError **)outError
{
//System Generated code here that I haven't touched but removed from example for readability
}
@end
While highlighting the first Table Column, in the Attributes tab I have Identifier set to variable personName. In the second Table column attributes tab I have Identifier set to expectedRaise.
Do I need to set the Class Identity of the NSTableView to 'Person'? It won't let me so I'm guessing no.
Do I need to Control-drag from the MyDocument object to the NSTableView? I did that and no luck.
What am I missing to make this needed connection?
Thank you.