Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jun 15, 2009, 09:53 AM   #1
mdeh
macrumors 6502
 
Join Date: Jan 2009
Hillegass Page 90 FTMC "Setting the target Programmatically"

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.

Code:
SEL mySelector;
mySelector = @selector(sayIt:);
[myButton setAction: mySelector];
The code to find a selector from from an NSString at runtime also seems simple.

Code:
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.
mdeh is offline   0 Reply With Quote
Old Jun 15, 2009, 06:15 PM   #2
HiRez
macrumors 601
 
HiRez's Avatar
 
Join Date: Jan 2004
Location: Western US
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.
__________________
Go outside, the graphics are amazing!
HiRez is offline   0 Reply With Quote
Old Jun 15, 2009, 07:06 PM   #3
mdeh
Thread Starter
macrumors 6502
 
Join Date: Jan 2009
Quote:
Originally Posted by HiRez View Post
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.
Quote:
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.
Quote:
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.
mdeh is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
All iPads: FYI - Target offering $200 store GC for "any" iPad trade-in jasonv1 iPad 84 Nov 10, 2013 10:41 AM
Need help setting "home" and "work" locations correctly dk143 iOS 6 4 Jun 23, 2013 08:03 PM
Thunderbolt and possible "Target Disk Mode" transfer alexglee MacBook Pro 2 Nov 19, 2012 10:49 AM
How i can fix "Wiki setting error" and "Running time error" ? Amethyst Mac OS X Server, Xserve, and Networking 0 Sep 2, 2012 02:34 PM
I forgot to set the version of "deployment target". fabiogc iPhone and iPod touch Apps 0 Aug 31, 2012 11:24 AM

Forum Jump

All times are GMT -5. The time now is 04:40 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC