Fun with button control


While fooling around with SpeakLine some more, I thought it would be interesting to try to turn off the speak button when there was no text in the field (like we turn it off when text is being spoken, or like we turn off the stop button when the app is not speaking). There is already a check for this in sayIt, but I wanted to try something pre-emptive. I set the delegate for the spoken text field to be the SpeakLineAppDelegate and added the following code (after poking around the docs a bit):

- (void)controlTextDidChange:(NSNotification *)obj { if ([[_textField stringValue] length] == 0) { [_speakButton setEnabled:NO]; } else { [_speakButton setEnabled:YES]; } }
Since it starts off with text already loaded in the field, I did not have to initially disable the button (either in code or in the IDE). I think since there is only one control that changes its text that I got away cheap here. Otherwise, I think I would have either had to add some logic to figure out what object sent the notification (using the NSNotification obj passed in) or had different delegation locations (separate helper objects?) for the various fields. Or something. Anyway, it seemed to work, and that was when I decided that was enough fun for the day… :smiley:


My solution to the text field being empty (because the user had deleted its contents) was to speak the value of the placeholder for the text field cell when the Speak key was pressed. I set the placeholder to “Please enter something to speak”.

When the text field was empty and the placeholder was being spoken, I disabled both the Speak and the Stop buttons.

Just another perspective. :slight_smile: