Implementing this by greying out Delete Button


#1

I decided to go a step further and do this in a very Mac-Like way. By this I mean that according to the HIG, when a row is NOT selected, the delete button should be greyed out (not selectable at all.) At first, I did like others did and simply checked to see if itemTableView.selectedRow >0 0 to avoid getting an error in deleting when nothing was selected. I then decided that I’d prefer to simply have delete not even active if no row was selected.

I implemented it by unselecting State:Enabled in the Interface Builder, so when TahDoodle launches, delete is greyed out. Then, I created a new IBAction: - (IBAction)selectArow:(id)sender; which I wired up my TableView. Now, when I click on the TableView, I do the check of itemTableView.selectedRow (or [itemTableView selectedRow] to keep the boss-man happy :wink: and now it works! I’m still having an issue with when I actually double click to edit, so I still have to make the check in the delete to make sure I’m not trying to delete while editing.

Trying to figure this one out. Aaron (or anyone, really, who knows), what is the PREFERRED method of activating/deactivating the delete button? There are many containers within containers in the TableView, so I’m not sure if I’m doing it the best way or not.

Still need to do the error stuff, then on to the next chapter.

This is a GREAT book! I’m learning a lot and feeling comfortable in exploring on my own.

Thanks again, Aaron and all the “Big Nerds” for a great resource!

Joe


#2

The trick is to become a delegate of the table view so that you get a message each time the selection changes:

- (void)tableViewSelectionDidChange:(NSNotification *)aNotification
To disable a button:

[myButton setEnabled:NO];
Don’t forget to reenable it!


#3

Thank you, Aaron. That works great!

Joe


#4

Is there a working source code to get this part of the program to work?


#5

Well, I think the code in BNRDocument.m would more or less just be:

- (void)tableViewSelectionDidChange:(NSNotification *)aNotification { NSLog(@"Index is %ld", [itemTableView selectedRow]); if ([itemTableView selectedRow] < 0){ [deleteButton setEnabled:NO]; } else { [deleteButton setEnabled:YES]; } }
(The NSLog was just for debugging). But you would need to make File’s Owner (BNRDocument) a delegate for the table view, and connect up an outlet for the Delete button by control-dragging from it to BNRDocument.h (I named it “deleteButton”). You’d also want to use the attribute editor to set the initial state of the delete button to be not enabled.

At least this works for me. I’m not holding it up as an example of the best way to do it, though.


#6

The button was created with interface builder.
But I can’t get its variable name.

Can you explain me how to get the variable name of an object created with Interface Builder.
An other problem, is how to display the code generated by interface builder.

Thank you.

PS: I’m afraid all the stuff is dynamically done at runtime from a descriptive file.
So, we can’t get any information, and can’t add code at some special place like in netbeans.
I’m wrong ?