advertisement

Article:
  Creating Toolbars for Mac OS X
Subject:   Enabling / Disabling toolbar items and buttons
Date:   2002-03-20 23:32:00
From:   michele
Finally, I've made some changes to enable / disable buttons and toolbars items in case of no selected records or selected records with no email addresses.


First I've added three outlets in IB for Add button, Delete button, and Send Mail button, and connected them to the controller.


Then I've declared the controller as delegate for the tableView in IB so that it responds to the tableViewSelectionDidChange method.


And I've added two methods:


- (void) tableViewSelectionDidChange: (NSNotification *) aNotification
{
[insertButton setEnabled: (([tableView numberOfSelectedRows] == 0) ? NO : YES)];
[deleteButton setEnabled: (([tableView numberOfSelectedRows] == 0) ? NO : YES)];
[sendMailButton setEnabled: [self validateSelectedEmails]];
[insertButton setNeedsDisplay: YES];
[deleteButton setNeedsDisplay: YES];
[sendMailButton setNeedsDisplay: YES];
}


so that the buttons are enabled or disabled according to selected records.


- (BOOL) validateSelectedEmails
{
NSEnumerator *e;
NSString *selectedEmailString;
NSNumber *index;
BOOL flag = YES;


// Check if there are selected rows
if ([tableView numberOfSelectedRows] == 0)
{
flag = NO;
return flag;
}


// Enumerate all selected rows
e = [tableView selectedRowEnumerator];


// Traverse the selected rows with enumerator
while ( ( index = [e nextObject] ) )
{
// Retrieve the selected records
if ([index intValue]<= [activeSet count] - 1)
{
selectedEmailString = [[activeSet objectAtIndex: [index intValue]] objectForKey: @"Email"];
if ([selectedEmailString isEqualToString: @""])
{
flag = NO;
return flag;
}
}
}


return flag;
}


and changed the following category method:


- (BOOL) validateToolbarItem: (NSToolbarItem *) anItem
{
if ([anItem action] == @selector(deleteRecord:))
{
return ([tableView numberOfSelectedRows] > 0);
}
else if ([anItem action] == @selector(insertRecord:))
{
return ([tableView numberOfSelectedRows] > 0);
}
else if([anItem action] == @selector(sendMail:))
{
return ([self validateSelectedEmails]);
}
else if ([[anItem label] isEqualToString: @"Search Records"])
{
[anItem setEnabled: ([records count] > 0)];
return ([records count] > 0);
}

return YES;
}


As for the dimmed colors, it's the fault of my poor eyes. As I'm a little colour-blind, I cannot see well changes of colours, unless I set contrast and luminosity to their maxima, which is not good in turn for my eyes;


Missing are methods to check valid names, valid email addresses and valid phone numbers, print the whole thing as a booklet, then a script to retrieve the already stored addresses in an old application (not even carbonized).


Well, when finished, it wil be the first useful (for me) application I ever wrote.


Many thanks to Mike and all fellows.


Michèle