The following steps will help us implement the SimpleCalc behavior:
Open the
SimpleCalc
project in Xcode.Click on the item named
BTSAppDelegate.m
and add an implementation for themyButtonAction:
method. The code that we need to add is as follows:/* Create the App implementation for the buttons */ - (IBAction)myButtonAction:(id)a_sender; { // For now, just beep // Comment out the beep, we don't need it // NSBeep(); // Get the button title (+, -, x, or ÷) to // determine which operation the App will // preform NSString *l_operation = [a_sender title]; // Get a double precision number from the // first text field double l_value1 = [mValue1 doubleValue]; // Get a double precision number from the // second text field double l_value2 = [mValue2 doubleValue]; // Create a double precision variable to // hold the result of l_value1 <op> l_value2 double l_result; // If the operation was addition, then // add the two values if ([@"+" isEqual: l_operation]) { l_result = l_value1 + l_value2; } // If the operation was subtraction, then // subtract the two values else if ([@"-" isEqual: l_operation]) { l_result = l_value1 - l_value2; } // If the operation was multiplication, then // multiply the two values else if ([@"x" isEqual: l_operation]) { l_result = l_value1 * l_value2; } // The operation must have been division, so // divide the first value by the second value else { l_result = l_value1 / l_value2; } // Set the result text field to the result [mResult setDoubleValue:l_result]; }
Tip
Downloading the example code
You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com . If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.