Cocoa bindings

Discussion in 'Mac Programming' started by Macgenie, Jan 8, 2008.

  1. Macgenie macrumors newbie

    Joined:
    Jan 8, 2008
    Location:
    Suffolk, UK
    #1
    I have an NSMatrix of buttons configured as a regular 0 - 9 keypad.
    I have set all the button tags to 0 - 9 - if I were using Interface Builder in the normal way I would instantiate a controller class, connect the keypad and then use the tags to tell my code what button had been pressed.

    I have been impressed with the bindings system and I would like to bind my matrix of buttons to an int in my model class but am stumped trying to figure out how to access the key tags through the binding mechanism.

    Any help would be appreciated !
     
  2. Eraserhead macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
  3. Macgenie thread starter macrumors newbie

    Joined:
    Jan 8, 2008
    Location:
    Suffolk, UK
    #3
    Reply to Eraserhead

    No - I started out with a simple Cocoa application, my main menu nib has an instance of NSController, I have a model class bound to the controller for various text views in the nib (which all work OK).

    What I am trying to do is to take the basic calculator app covered in the O'Rielly Cocoa programming book and trying to modify the app to use the bindings system instead of outlets and actions (which were not in use at the time the book was produced).

    Once I have learnt how to do this I will be master of the universe (soon)....:)
     
  4. Eraserhead macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #4
    I guess you bind the matrix to the relevant key in the NS Controller...
     
  5. Macgenie thread starter macrumors newbie

    Joined:
    Jan 8, 2008
    Location:
    Suffolk, UK
    #5
    Thanks Eraserhead, but that's what I've been trying to do!
    Every key value I try compiles OK but I get runtime exceptions saying my class is not key-value compliant.

    Looking at some of the Cocoa header files & the documentation with XCode I can see a method using :selectedTag - if I could access this somehow via the bindings I might be home & dry.

    In the usual way using outlets etc and an instantiated nib class I would have a method containing code such as

    - (IBAction)enterdigit:(id)sender
    X = (X * 10.0) + [[ sender selectedCell] tag];
    [self displayX];

    - this takes the value stored in x, shifts the digit left (by multiplying by 10) and then adds in the key stroke to x by referencing the selected cell key's tag (which is set equal to the visible numerical value on each key).

    I am trying to replace this code with the bindings system but I can't see how to pass in the tag value of the key - its my setter method that's at fault, I'm sure.

    Some examples in the documentation from Apple wouldn't have gone amiss ....
     

Share This Page