MOBILE

iPhone Application Development : Building a Multi-View Tab Bar Application (part 3) - Implementing the Volume View

3/19/2011 4:37:36 PM

Implementing the Volume View

In the volume view, the application will accept input for the dimensions of a box (length, width, height) and a sphere (radius) and calculate the volume of the object based on these values. The interface elements will be largely identical to the area view but will require an additional field (height) for the box calculation. Begin the implementation by editing the VolumeViewController header.

Adding Outlets and Actions

With the exception of some terminology changes required by our switch from 2D to 3D, the outlets and actions that will be required in the volume view should be very familiar:

boxWidth (UITextField): Field for entering the width of a box

boxLength (UITextField): Field for entering the length of a box

boxHeight (UITextField): Field for entering the height of a box

sphereRadius (UITextField): Field for entering the radius of a sphere

boxResult (UILabel): The calculated area of the rectangle

sphereResult (UILabel): The calculated area of the circle

summaryViewController (SummaryViewController): The instance of the summary view

calculate (method): Performs the area calculation

hideKeyboard (method): Hides the onscreen keyboard

Edit the VolumeViewController.h file to include the necessary outlets and actions for the view. When finished, your code should look like Listing 4.

Listing 4
 2: #import "SummaryViewController.h"
3:
4: @interface VolumeViewController : UIViewController {
5: IBOutlet UITextField *boxWidth;
6: IBOutlet UITextField *boxHeight;
7: IBOutlet UITextField *boxLength;
8: IBOutlet UITextField *sphereRadius;
9: IBOutlet UILabel *boxResult;
10: IBOutlet UILabel *sphereResult;
11: IBOutlet SummaryViewController *summaryViewController;
12: }
13:
14: @property (retain, nonatomic) UITextField *boxWidth;
15: @property (retain, nonatomic) UITextField *boxHeight;
16: @property (retain, nonatomic) UITextField *boxLength;
17: @property (retain, nonatomic) UITextField *sphereRadius;
18: @property (retain, nonatomic) UILabel *boxResult;
19: @property (retain, nonatomic) UILabel *sphereResult;
20:
21: -(IBAction)calculate:(id)sender;
22: -(IBAction)hideKeyboard:(id)sender;
23:
24: @end


Because of the similarity to the area view, we won’t go into detail on the individual lines. Let’s move on to the volume view itself.

Creating the View

Like the layout of the area view, the volume view will collect data, provide the user with a Calculate button and, of course, display the results. Figure 9 shows a finished version of the view.

Figure 9. Once again, collect data, calculate, and provide the results!


By the Way

The volume view includes images to help users identify the values that they will need to enter. If you didn’t drag the SphereVolume.png and BoxVolume.png images to your Xcode resources when building the area view, this is a good time to add them!


Open VolumeView.xib in Interface Builder and drag four text fields to the view. For the volume calculations, three fields should be grouped for length, width, and height of the box. A single field is all that is required for the sphere. Be sure to set the field attributes so that the keyboard displayed is a number pad. Add labels to the view to identify each of the fields for the user.

Position two additional UILabel instances below the box/sphere input fields—these will be used for the output of the calculations. Be sure to clear the default contents of these labels, or set them to 0. Using the Attributes Inspector (Command+1), size all the labels and fields appropriately.

To add the images, drag image views from the Library to the view. Set the contents of the image views by opening the Attributes Inspector (Command+1) for each view and choosing the BoxVolume.png or SphereVolume.png images.

Finish the view by adding two buttons: the Calculate button at the bottom of the view and the invisible button used to hide the keyboard. Remember to expand the “hide keyboard” button to fill the view and send it to the back. Open the Attributes Inspector (Command+1) for the button, and choose Custom for the button type to make the button invisible.

Connecting Outlets and Actions

Connect the fields, labels, and buttons in Interface Builder to the appropriate outlets that you created in the VolumeViewController.h file. Control-drag from the File’s Owner icon in the Document window to each of the input fields and output labels; choose the appropriate outlet when prompted.

To connect the button controls to the actions, Control-drag from the two UIButton instances to the File’s Owner, choosing the fitting calculate and hideKeyboard method when prompted.

Implementing the Volume Calculation Logic

Switch back to Xcode and edit the VolumeViewController.m file. As before, define the Pi constant at the top of the file:

#define Pi 3.1415926

Next, use the @synthesize directive to create the getters and setters after the @implementation directive:

@synthesize boxWidth;
@synthesize boxHeight;
@synthesize boxLength;
@synthesize sphereRadius;
@synthesize boxResult;
@synthesize sphereResult;

Create and edit the calculate method to determine the volume of the two shapes. For the box, this is length×width×height, and for the sphere, 4/3×Pi×R^3. Use the results to populate the boxResult and sphereResult labels in the view. Our implementation of this method is provided in Listing 5.

Listing 5.
-(IBAction)calculate:(id)sender {
float floatBoxResult=[boxWidth.text floatValue]*
[boxLength.text floatValue]*[boxHeight.text floatValue];
float floatSphereResult=(4/3)*Pi*[sphereRadius.text floatValue]*
[sphereRadius.text floatValue]*[sphereRadius.text floatValue];
NSString *stringBoxResult=[[NSString alloc]
initWithFormat:@"%1.2f",floatBoxResult];
NSString *stringSphereResult=[[NSString alloc]
initWithFormat:@"%1.2f", floatSphereResult];
boxResult.text=stringBoxResult;
sphereResult.text=stringSphereResult;
[stringBoxResult release];
[stringSphereResult release];

[summaryViewController updateTotal];
}


By the Way

Because only the logic for the calculations has changed, you should refer to the area view calculation for a detailed description of the methods used here.


Add the hideKeyboard method so that the user can dismiss the onscreen keyboard by touching the background of the view:

-(IBAction)hideKeyboard:(id)sender {
[boxWidth resignFirstResponder];
[boxLength resignFirstResponder];
[boxHeight resignFirstResponder];
[sphereRadius resignFirstResponder];
}

Finally, release the objects in the dealloc method:

- (void)dealloc {
[boxWidth release];
[boxHeight release];
[boxLength release];
[sphereRadius release];
[boxResult release];
[sphereResult release];
[super dealloc];
}
Other  
  •  Windows Phone 7 Development : Working with Video (part 2) - Coding the Application
  •  Windows Phone 7 Development : Working with Video (part 1)
  •  Windows Phone 7 Development : Plotting an Address on a Bing Maps Map and Working with the Bing Maps Service
  •  Windows Phone 7 Development : Using GeoCoordinateWatcher and the Bing Maps Control to Track Your Movements
  •  iPhone Application Development : Creating a Multi-View Toolbar Application (part 3) - Adding Toolbar Controls
  •  iPhone Application Development : Creating a Multi-View Toolbar Application (part 2) - Instantiating the View Controllers
  •  iPhone Application Development : Creating a Multi-View Toolbar Application (part 1)
  •  Windows Phone 7 Development : Using Location Services - Simulating the Location Service
  •  Introducing the Windows Phone Location Service and Mapping APIs
  •  iPhone Application Development : Implementing a Custom Picker View (part 4) - Tweaking the Picker UI
  •  iPhone Application Development : Implementing a Custom Picker View (part 3) - Reacting to a Picker View Choice
  •  iPhone Application Development : Implementing a Custom Picker View (part 2)
  •  iPhone Application Development : Implementing a Custom Picker View (part 1)
  •  Windows Phone 7 Development : Isolated Storage - Working with Isolated Storage Settings
  •  Mobile Application Security : WebOS Security - Permissions and User Controls
  •  Mobile Application Security : WebOS Security - Code Security
  •  Windows Phone 7 Development : Working with Isolated Directory Storage (part 2)
  •  Windows Phone 7 Development : Working with Isolated Directory Storage (part 1)
  •  iPhone Application Development : Making Multivalue Choices with Pickers - Using Date Pickers (part 3)
  •  iPhone Application Development : Making Multivalue Choices with Pickers - Using Date Pickers (part 2) - Adding a Date Picker
  •  
    Top 10
    How To Basics Installing Linux
    How To Basics Virtualization
    Nero 12 Platinum Multimedia Software
    Ten Popular Open Source Media Players
    Top Mind Mapping Tools For Android
    Ultra-portable Devices Eat Into The Legacy PC Market (Part 3)
    Ultra-portable Devices Eat Into The Legacy PC Market (Part 2)
    Ultra-portable Devices Eat Into The Legacy PC Market (Part 1)
    JBL Flip Portable Wireless Loudspeaker
    AOC E2460phu LED LCD 24” Monitor
    Most View
    Gigabyte GA-Z77N-WiFi – A Motherboard With Plentiful Technological Features
    The big test … Inter Core Power (Part 1) - Acer Aspire Ethos 8951G
    Kyocera Mita FS-C5150DN
    Lenovo ThinkPad Tablet 2 (Part 1)
    Toshiba Satellite U925t Review (Part 2)
    Windows 7 : Keeping Your Family Safe While Using Your Computer (part 1)
    Windows 7 : Zero Touch Installations - Deploying Windows 7 (part 3) - Performing the Zero Touch Installation
    Algorithms for Compiler Design: REGULAR GRAMMAR
    Buyer’s Guide: Touchscreen Monitors for Windows 8 (Part 3) : Dell S2340T, Elo 2201L
    How To Make The Most Of Dropbox (Part 1)
    NZXT Source 210 Elite - Finest Cases For Frugal Gamers
    Introduction to Windows 8 Administration : Deploying Windows 8
    How To Buy A Tablet
    The .NET Security Architecture
    Canon ImageFORMULA DR-M140 - A Compact And Versatile Solution
    Programming Microsoft SQL Server 2005 : Querying XML Data Using XQuery (part 1) - XQuery Defined
    Windows Server 2003 : Domain Name System - Forwarding
    Armaggeddon Mikoyan Foxbat, Shteelseries Sensei Major League Gaming Edition, Logitech G600
    10 Valuable TVs For Your Living Room
    SQL Server 2008 : Performance Tuning - Using Dynamic Management Views