Adding the Unary Minus Function to the Controller Class
We want the unary minus function (the
button with the “+/-” on it) to
change the sign of the number currently displayed in our
Calculator’s numeric display area. One way to
implement this function is to handle it with another case in the
switch
statement in the enterOp: method — we could give the
“+/-” key its own tag and have the
enterOp: method intercept it and
perform the appropriate function. The problem with this approach is
that the unary minus function has little in common with the other
arithmetic functions: it takes one argument instead of two, and it
operates immediately on the displayed value. A far better way to
implement this function is to implement a new action method in the
Controller class.
Using IB’s Read Files Commandwith a New Action Method
Adding new action methods to existing classes is slightly more
difficult than creating the initial class definition. Early versions
of IB simply replaced the existing class files
(Controller.h
and
Controller.m
, here) with new versions, wiping out any source code that you might have added. Current versions of IB detect that you have made changes in the class files and allow you to merge the changes using the Merge Files application. Unfortunately, this can be a painful and error-prone process. The safest way to add new outlets and actions to files after they have been created and edited is to add these items directly to the Objective-C interface files in a text editor, ...
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.