Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

jedirunner

macrumors member
Original poster
Aug 1, 2007
32
0
Hey guys/gals,

I have been going even further into cocoa programming and trying to get a calculator program to work (sounds simple, I know). I have a few questions for you:

1- I have hooked up several buttons to a single button handler. In debugging into the code, I can see that my handler gets called for all of the button presses. Now the question is how to detect which of the buttons was clicked? I see that I get a single parameter to the button handler "(id) sender". Just how do I use the sender parameter to determine the button that triggered the event?

2- I have an NSTextView to allow the user to directly type the numbers for the calculations. I need to filter on the keys to only allow certain key events to actually affect the displayed text. More correctly, I need to filter keys, and take on of the following actions:
a. If it's a number, append it to the number in the display, and update the display.
b. If it's an operator, process the operator and reset the display.
c. If it's not one of those, then ignore it.
So how do I do this type of thing? Perhaps, upon thinking about it, it's not really NSTextView that I need, but rather a similarly-displayed label with a border around it.

3- How do I interpret key presses on the window itself, rather than forcing the user to type into a text field? Or, if I switch away from using a text field, how do I intercept key presses for the window itself so I can process them? If I get a key press for the number "1", can I force the button to look like it's been clicked?

4- I imagine much of my terminology is wrong in how I'm using it now that I'm coding in the mac world. Feel free to tell me what terms I should use differently so I can catch up with you all and not appear to be as much of a noob as I really am on the mac front. :)

Thanks very much to all of you!

Kevin
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
1- I have hooked up several buttons to a single button handler. In debugging into the code, I can see that my handler gets called for all of the button presses. Now the question is how to detect which of the buttons was clicked? I see that I get a single parameter to the button handler "(id) sender". Just how do I use the sender parameter to determine the button that triggered the event?

You can assign "tags" (just a number) to a button in Interface Builder. Then you can check for that tag by calling [sender tag]

3- How do I interpret key presses on the window itself, rather than forcing the user to type into a text field? Or, if I switch away from using a text field, how do I intercept key presses for the window itself so I can process them? If I get a key press for the number "1", can I force the button to look like it's been clicked?

My first idea would be to subclass NSView and override the keyDown: events that NSResponder calls. Then you can override the default content view of a window with NSWindow's setContentView: method.

Not sure about #2 off hand.
 

Columbo X

macrumors member
Jun 10, 2007
62
0
UK
To answer your second question, you can assign a delegate to the text view and then implement the relevant delegate methods - this gives you a great deal of control over the behaviour of the text view and allows you to handle keypresses on behalf of the text view (checkout the Apple docs for the NSTextView delegate methods and what they do).
 

jedirunner

macrumors member
Original poster
Aug 1, 2007
32
0
Thanks!

Thanks very much to both of you for your answers. I'll give those methods a try and see where it gets me.

With each passing trivial sample program I write, I'm getting more and more used to the environment (xcode/IB) and having more fun. Maybe one day soon I'll be able to switch my side projects away from C# on Windows, and move them over to Mac. :)

Thanks again,

Kevin
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.