A can of colours

Last night was the first Croydon Tech City programming surgery of the new year. On several of these events last year, I forgot to write up the questions and learning. So, this time I decided to keep notes as we went along to remind me what I needed to write up.

As with most of our sessions so far, our mission was to battle our way through Apple’s xcode interface builder and get it to do our bidding. We set ourselves the following objectives for the night:

  • add a menu item
  • open a window when menu item is selected
  • trigger some Swift or C++ code when a menu item is selected

Adding the menu item was a simple enough process: find the menu item control, drag and drop it onto the provided menu bar, and name the item. Easy!

Our next objective was to get that menu item to do something. The first idea was to get it to open a colour picker window. And here’s where we hit the, by now, familiar brick wall of Apple’s obtuse developer documentation. After some considerable digging, it appeared that opening a colour picker window was a built in feature of applications created in the interface builder. So you can create a new application in Xcode, compile and run it, and press shift command C and up it pops.

However, what we really wanted here was the ability to open the colour picker from our menu item. And we already had our new menu item and a way to open the colour picker. All we required was a way to connect it. Attempting that took the rest of the night. (In fact I didn’t find a solution until after the event.)

I won’t list all of the things we tried, or all the many pages of documentation we skimmed through looking for an answer to this seemingly trivial problem. Instead, I’ll skip to the end.

To open the colour picker from a menu item:

  • click on the menu item
  • connect Sent Actions “action” to the Application
  • choose orderFrontColorPanel: in the menu that pops up
  • compile and run

Of course, once I’d finally worked this out, I discovered that there was already a menu item that did exactly this in the Format sub menu of the Font menu. And this is given to you by default. But it was a relief to have figured it out for my own peace of mind.

My Tech City programming surgery currently runs monthly on the last Thursday from 7pm to 9pm at Matthews Yard. There’s no charge to attend. If you have a programming problem that has you flummoxed, or you’d just like some help learning to program, come along and we’ll give you a hand.

Leave a Reply

Your email address will not be published.