Search code examples
objective-ccocoainterface-buildercocoa-bindings

Is assigning the same Action-method to multiple Cocoa UI objects e.g. NSButton possible?


I'm currently learning ObjC and Cocoa programming, coming from the Java world. To test my current skills and learning progress I'm creating a small calculator app from scratch (OSX not iOS).

My UI has 10 digit buttons 0-9 among others.

My first thought was, since the action receives the senders reference, to make one action like -(IBAction)captureDigit:(id)sender and then just grab the digit from the button title. But the interface builder only allows an action to be connected with one sender it seems.

So I ended up creating 10 captureDigit actions in my controller.

My Question: is the first option possible somehow? I thought of adding the actions programmatically (is this possible?) to the buttons, but then I would have to add all digit buttons as outlets to my controller.

Bonus Question: can a NSButton hold some kind of non visible value? Could not find this in the documentation. Maybe this would violate the MVC pattern as the UI would then know of application specific data?

Thanks for any useful and kind answer in advance, I'm still learning


Solution

  • You can definitely connect more than more button to a single action. Also, you can use the tag field of any object to give it a "behind the scenes" value.