PDA

View Full Version : Hillegass Page 90 FTMC "Setting the target Programmatically"




mdeh
Jun 15, 2009, 08:53 AM
Hi all,
On page 90 of AH (3rd ed) there is a "For the more curious" section. The section demonstrates how to implement "Setting the target Programmatically".
I had initially just accepted this bit of information, but then a reader had tried to implement this and like myself, run into a dead end.

Here is the setup to "set the action of a button to the method" sayIt. (The chapter introduces an application that speaks a line of text). The code seems simple enough.

SEL mySelector;
mySelector = @selector(sayIt:);
[myButton setAction: mySelector];


The code to find a selector from from an NSString at runtime also seems simple.


SEL mySelector;
mySelector = NSSelectorFromString(@"sayIt");
[myButton setTarget:someObjectWithAsayItMethod];
[myButton setAction:mySelector];


But, trying to implement this is not as easy ---for me at least.

So, specifically, where would one place the above code? ( The "SpeakLine" application is essentially a window with 2 buttons (StartSpeaking) and (Stop) and an NSTextField that holds the string to be spoken PLUS an object ( the AppController) that has the outlet to the NSTextField and defines the above two methods). I am assuming ( perhaps incorrectly) that this could be implemented in this application?
If that is too vague, ( and it probably is) a few pointers as to how to implement this would be appreciated.

Thanks in advance.



HiRez
Jun 15, 2009, 05:15 PM
I'm not sure I understand exactly, but it seems odd to have two buttons for start and stop (speaking). Wouldn't it make more sense to have a single button that toggles between start and stop? If the action of the button doesn't need to change throughout the program, you'd probably implement it in awakeFromNib: in your AppController object (where all UI elements in the .nib/.xib file are guaranteed to be valid). Of course it'd be easier to just connect it in IB but that's not the point here. If it does need to change (the reason you might want to be setting it programmatically), then you just put the code wherever it makes sense to change it. For example, if you had the single button toggle model, connect that button to an IBAction method startSpeaking: (because speaking will be off when the app launches). Inside startSpeaking: you would set the button action to stopSpeaking: and do the opposite inside that method.

mdeh
Jun 15, 2009, 06:06 PM
I'm not sure I understand exactly, but it seems odd to have two buttons for start and stop (speaking).

I agree that a single button would make more sense, but as this is very early on in Hillegass's book, I imagine the "2 button" view makes it clean and easy.
If the action of the button doesn't need to change throughout the program, you'd probably implement it in awakeFromNib:
One of the things I realize now is that the issue about "programmatic" vs IB is that both of these ways of doing it "establish the connection" between view and specific action. That's the part I was (Redfaced) missing. :)
If it does need to change (the reason you might want to be setting it programmatically), then you just put the code wherever it makes sense to change it..... if you had the single button toggle model, connect that button to an IBAction method startSpeaking: (because speaking will be off when the app launches). Inside startSpeaking: you would set the button action to stopSpeaking: and do the opposite inside that method.
Aha!! So, you would have 2 methods connected to a single control? Each time that method is called, it sets the button to the opposite action, and completes what it was called to do in the first place? If that is correct, thanks so much.