More cocoa questions : NSButton and NSTextView

Discussion in 'Mac Programming' started by jedirunner, Sep 6, 2007.

  1. jedirunner macrumors member

    Joined:
    Aug 1, 2007
    #1
    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
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    You can assign "tags" (just a number) to a button in Interface Builder. Then you can check for that tag by calling [sender tag]

    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.
     
  3. Columbo X macrumors member

    Joined:
    Jun 10, 2007
    Location:
    UK
    #3
    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).
     
  4. jedirunner thread starter macrumors member

    Joined:
    Aug 1, 2007
    #4
    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
     

Share This Page