Chapter 7. Delegation and Resizing

In this chapter, we will modify our Calculator application so that a user can choose to work with any of the following bases: base 2, 8, 10, or 16. To do this, we’ll modify the Controller class to keep track of the current base and update the display accordingly. We’ll also have to modify the keyboard-input routines to work with the proper base, called the radix , and ignore keypresses (digit-button clicks) that are invalid for a particular base. Most importantly, we will introduce the concept of delegation , a technique for specifying objects that perform functions for other objects. As for the user interface, we’ll set up a pop-up menu (for the user to change the base) and show how to resize a window programmatically.

Handling Different Bases

The first step toward making our Calculator work with more than one base is to put a control for changing the base in the Calculator window. We’d like to use a radio button control, for several reasons. First, the radio button allows only one selection at a time, which is how our calculator will work. Second, it’s both an input and an output at the same time — it shows a state and lets you change it. In addition to letting the user change the base, the radio button matrix indicates which base is currently selected and shows at a glance all of the choices. Unfortunately, the radio button idea has one major problem — it will take up too much room in our small calculator window. Instead, we’ll use a pop-up ...

Get Building Cocoa Applications: A Step by Step Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.