objective c – Making memory functions buttons for a calculator app

objective c – Making memory functions buttons for a calculator app

As others have suggested, you need some model structure (e.g. some class properties) to keeping track of where you are in your calculation. But if you need to keep track of your calculators memory register, you might have a property like:

@property (nonatomic) double memoryValue;

So you would just take that property, e.g. memoryValue, and either add to it, subtract from it, clear it (set it to zero), or retrieve its value, depending upon whether the user hit m+, m-, mc, or mr, respectively. I would personally even contemplate a NSNumber property, but I dont think you need to go there.

Maybe Im not understanding your question.

I have to confess that I was more worried by your line of code where youre getting your currentValue from screenLabel.text. You really do not want to retrieve data that is intrinsic to the operation of your app from a user-interface control like a UILabel, which is solely designed for the presentation of information. This implies a fundamental confusion about how MVC, or Model-View-Controller, apps are designed. The values that drive the calculations (e.g. the current value, the memory value, etc.) are part of the model (possibly stored as properties of some model object). The UILabel that presents the currently calculated value, is part of the view.

Its not a good idea to use the UILabel (part of the view) to drive calculations. Your app should be maintaining a model for the apps data/operations, and the view should just be a presentation of that model (not the other way around).

A calculator app is a wonderful example of the practical considerations here. For example, your calculator may have a scientific/exponential notation mode (where 1,200,000,000 might be displayed as 1.2e9). Or maybe its displaying the numeric value with a comma as a thousands separator (where one million is displayed as 1,000,000.00). The formatting of the text property of the UILabel could be driven by a variety of different factors, and you really shouldnt be worrying about the string formatting when using double values for calculation purposes.

Another great example is the number of digits shown is a function of whether the app is in landscape orientation (where we have space for lots of decimal places) versus portrait orientation (less space and therefore fewer decimal places shown). The app shouldnt remember fewer digits just because there was less space to show them on the portrait-oriented screen. The actual calculated values in the underlying model shouldnt change as the user interface of the view changes. And, regardless, the model probably will always use more significant digits that youll ever want to present in a user interface.

But hopefully these examples drive home the distinction between your apps model and the view. In short, its not prudent to retrieve values from UILabels. Keep the real values (no pun intended) in your apps model.

A stack data object in your model would be able to persist values in memory.

Check out this category:

objective c – Making memory functions buttons for a calculator app

Still guessing what your problem really is, here is an answer to what you appear to have asked…

Your calculator presumably has an add button, and addition requires two operands. Your question indicates that one of those is stored in screenLabel as text and currentValue as a number. Where is the other one? Store your memorised value in a variable in the same object you store that second value.

E.g. youll have something like this:

@implementation MyCalculator
   double currentValue;
   double previousValue;
   double memoryValue;



Leave a Reply

Your email address will not be published.