advertisement

Article:
  Creating Toolbars for Mac OS X
Subject:   Some changes to code
Date:   2002-03-18 16:43:47
From:   michele
Hello Mike and nice fellows,


A very good article to deal with !!!


As usual, some changes in code.


In text, following code breaks into debugger (toolbar released twice), when clicking on customization icon or hitting the corresponding menu item:


- (void) setupToolbar
{
NSToolbar *toolbar = [[NSToolbar alloc] initWithIdentifier: @"mainToolbar"];
[toolbar autorelease];
[toolbar setDelegate: self];
[toolbar setAllowsUserCustomization: YES];
[toolbar setAutosavesConfiguration: YES];
[mainWindow setToolbar: [toolbar autorelease]];
}


In order, to have the toolbar visible at launch time, I've changed the code to:


- (void) setupToolbar
{
NSToolbar *toolbar = [[[NSToolbar alloc]
initWithIdentifier: @"mainToolbar"] autorelease];
[toolbar setDelegate: self];
[toolbar setAllowsUserCustomization: YES];
[toolbar setAutosavesConfiguration: YES];
[mainWindow setToolbar: toolbar];
}


Instead of defining / implementing the (void) setupToolbar method in the ToolbarDelegateCategory.h / .m, I've implemented it directly in the Controller to avoid warning message (NSController does not respond to setupToolbar method).



In the validating toolbar menu item section, the following code generates a warning message when building (no explicit return when the action is not delete records):


- (BOOL) validateToolbarItem: (NSToolbarItem *) theItem
{
if ([theItem action] == @selector(deleteRecord:))
{
return ([tableView numberOfSelectedRows] > 0);
}
}


To avoid warning, I've changed the code to:


- (BOOL) validateToolbarItem: (NSToolbarItem *) theItem
{
if ([theItem action] == @selector(deleteRecord:))
{
return ([tableView numberOfSelectedRows] > 0);
}

return YES;
}


And I've found a not too elegant way to resize the columns automatically at launch time and when the user changes the items displayed in the tableView to fit the whole area:


- (void) initializeTable: (NSTableView *) aTableView withColumns: (NSArray *) identifiers
{
NSEnumerator *e;
id identifier, column;
NSTableColumn *aTableColumn;
// Added code
float totalSize = 0.0;


// Clear out the existing columns in table view
e = [[aTableView tableColumns] objectEnumerator];
while ( ( column = [e nextObject] ) )
{
[aTableView removeTableColumn: column];
}


// Add columns from the argument array
e = [identifiers objectEnumerator];
while ( ( identifier = [e nextObject] ) )
{
aTableColumn = [tableColumns objectForKey: identifier];
[aTableView addTableColumn: aTableColumn];
[[checkBoxes objectForKey: identifier] setState: NSOnState];
// Added code
[aTableColumn setMinWidth: 200.0];
[aTableColumn setWidth: [aTableColumn minWidth]];
[aTableColumn setMaxWidth: 1000.0];
totalSize += [aTableColumn width];
}


// Resize the column to fill the whole area
if (totalSize < [aTableView frame].size.width)
{
[aTableView sizeLastColumnToFit];
}
// End of added code
}


- (void) saveTableColumnPrefs
{
id column;
NSEnumerator *e = [[tableView tableColumns] objectEnumerator];
// Added code
float totalSize = 0.0;


// Clear out the array
[userColumns removeAllObjects];


// Repopulate the array with the identifiers of the columns currently contained in the table
while ( ( column = [e nextObject] ) )
{
[userColumns addObject: [column identifier]];
// Added code
[column setMinWidth: 200.0];
[column setWidth: [column minWidth]];
[column setMaxWidth: 1000.0];
totalSize += [column width];
}


// Resize the column to fill the whole area
if (totalSize < [tableView frame].size.width)
{
[tableView sizeLastColumnToFit];
}


// End of added code

// Associate userColumns with the object at key User Columns
[prefs setObject: userColumns forKey: @"User Columns"];
}


Michele