Disabling Buttons for BetterMultiradix Input

You may have noticed that there is still a big problem with our Calculator — the keypad doesn’t work correctly in any base except for decimal. The reason for this failure lies with the following statement in the enterDigit: method:

X = (X*10.0) + [ [sender selectedCell] tag];

This statement multiplies whatever is in the X register by 10 and adds the tag of a digit button each time one is clicked. Unfortunately, we don’t want to multiply the X register by 10 if a radix other than base 10 is in effect; instead, we want to multiply by the current radix. So, for a first pass, the 10.0 in this statement should be replaced with radix.

  1. Replace the 10.0 in the enterDigit: method in Controller.m with radix to get:

    X = (X*radix) + [ [sender selectedCell] tag];

But that’s not the only change we need to make; we also have to change the keypad of buttons so that particular buttons are deactivated when certain bases are selected. For example, a user shouldn’t be able to press the 8 button when the Octal base is chosen. Also, it would be nice to make buttons for the numbers A, B, C, D, E, and F appear when the user selects Hex. We’ll address all of these problems and add the new features in the remainder of this chapter.

Accessing NSMatrix Cells with an NSArray Object

Every Cocoa button is either enabled or disabled. If a button is disabled, the black labeling on it turns gray, and the button won’t respond to the mouse. In the following steps, we’ll ...

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.