As many of us are not familiar with software development, here’s some extra tips to help you out on some key parts of this assignment
Float-point number
Floating-point number should be used instead of integer. We should perform all calculation using “double” data type to simplify our work for this assignment.
Storing input value
All of us have used calculator before. We probably haven’t paid much attention to its behavior though.
Let’s study the case where you are entering the value “123″ to a calculator.
The sequence of keys you will type is as follow:
This key pattern needs to be translated into a numeric value that is stored in your iPhone as “123″
Let’s study the integer value. We define a variable of type “double” in the view controller class.
double inputValue;
When user taps “1″ key, the value stored in “inputValue” is 1.
When user taps “2″ key, we should…
inputValue = inputValue * 10.0 +;
Since inputValue is current “1″ and user is pressing key “2″, the above equation will becomes:
inputValue = 1.0 * 10.0 + 2.0
inputValue will be storing 12.
When user taps “3″ key, the same should formula should be used.
Now, let’s consider the case user enters “0.987″.
This time the situation is different.
When user tap “0″ key, inputValue = 0.0
When user taps “9″ key, we should use this formula instead:
inputValue = 9.0 / 10.0 + inputValue
When user taps “8″ key,
inputValue = 8.0 / 100.0 + inputValue
We need to keep track of which decimal place user is entering. Therefore, we need to introduce an extra variable to store that.
double decimalValue = 0.0;
when user has tap “.” key, set…
decmialValue = 10.0;
From here, whenever the user taps a numeric key, multiply decimalValue by 10.
decmialValue *= 10.0;
As you can see, the logic to handle the integer part and the decimal part are quite different. I would suggest to use 2 variables to store values instead of storing both parts of the value in the same “inputValue” variable.
Put the following in your view controller header file.:
double integerStore, decmialStore;
Identify what key is pressed
How can we identify what key the user has tapped?
I’ll suggest either one of the following case:
- Set the “tag” property in UIButton from interface builder. Or,
- Do a string comparison
If you use the first method, you will get a bonus. Please refer to the assignment sheet.
The 2nd method is more straight forward. The label in the button shows the vale already. However, that value is a string. We need to identify the string and make that a numeric value.
You can use titleForState: to get the string value. Then, perform a string comparison as follow:
- (IBAction)keyPressed:(id)sender {
// cast the sender into a button
UIButton * btn = sender;
if ( [[btn titleForState:UIControlStateNormal] isEqualToString:@"1"] ) {
// do something
} else if ( [[btn titleForState:UIControlStateNormal] isEqualToString:@"2"] ) {
// do something
} // continue the "if" condition for all keys
NSLog(@"Key code pressed: %d", btn.tag);
}
String formatting
User input and calculation result are output to a UILabel. Read documentation of UILabel. UILabel accepts only string value.
We need to convert the result to a string.
Use “stringWithFormat” to convert double into a string. Read String Format Specifiers in String Programming Guide documentation for that.
yourLabel.text = [NSString stringWithFormat:@"%d", integerStore + decimalStore]
Button Background
To customize button look-and–feel, follow the steps here.
You need the button background image, download stuff from class web site.
You need to prepare 2 sets of graphics – one for Retina display (hi-res) and one for standard resolution. Make sure you import the 2 versions of graphics to Xcode. The graphic assets I prepared contain both set of images.
If you need an image button_bg.png, the hi-res version of it MUST be named button_bg@2x.png
If you wanna know know, read here for details about preparing graphics for retina display check the following: