Implementing the Summary View
Of all the views, the
summary view is the easiest to implement. This view will provide a
single count of the number of calculations performed (as determined by
the number of times the Calculate button is pressed). Just a single
outlet and a single counter—no problem!
Adding the IBOutlet, Instance Variable, and Method
The SummaryViewController class will need a single outlet, totalCalculations, that will be connected to a UILabel in the summary view and used to display the calculation summary to the user. It will also use a single integer value, calcCount, to internally track the number of calculations performed.
Finally, the class will implement an instance method updateTotal that will update the calcCount value. Edit the SummaryViewController.h file to include these requirements, as demonstrated in Listing 6.
Listing 6.
#import <UIKit/UIKit.h>
@interface SummaryViewController : UIViewController { IBOutlet UILabel *totalCalculations; int calcCount; }
@property (retain, nonatomic) UILabel *totalCalculations;
-(void) updateTotal;
@end
|
Creating the View and Connecting the Outlet
To create the summary view,
open the SummaryView.xib file in Interface Builder. As promised, this
view is extremely easy to set up. Drag a UILabel object to the view. This will serve as the output of the total calculation count, so set the default text of the label to 0.
Finish the view by adding another label with the text Total Calculations: positioned above or beside the output label. The result should be similar to the view pictured in Figure 10.
Connect the output label to the totalCalculations outlet by Control-dragging from the File’s Owner icon to the UILabel instance within the Interface Builder Document view.
Connecting the Area, Volume, and Summary Views
If you recall, the previous two views call the updateTotal method of the summary view. For these views to have access to the summaryViewController instance variable, we must create two additional connections—this time in the MainWindow.xib file.
Open the MainWindow.xib
Document window and expand the tab bar controller hierarchy.
Control-drag from the area view controller instance to the summary view.
Choose the summaryViewController outlet, as demonstrated in Figure 11.
Repeat this for the volume view controller. The area and volume view controllers can now successfully call the updateTotal method.
Implementing the Volume Calculation Logic
All that remains for the
TabbedCalculation project is to implement the logic to track the
calculation total and update the summary view. Let’s open the
SummaryViewController.m file and wrap this up! First use the @synthesize directive to create the getter/setter for the totalCalculations UILabel:
@synthesize totalCalculations;
Next, add the updateTotal method so that it will increment the calcCount variable when invoked:
-(void) updateTotal {
calcCount++;
}
Now the tough part. Notice that we don’t display the new total in the updateTotal method? The reason for this is that until the view is displayed, there aren’t any UILabel values to update, so if we try to update before the view is shown, the count will be wrong. Subsequent views would work, but initially the displayed result would be incorrect.
So
how do we get around this? The easiest way is to update the view before
it is displayed onscreen. At that point in time, we have access to all
the objects, so everything will be copacetic. Overriding the viewWillAppear: method will provide us with the right hook into the display process. Implement the viewWillAppear: method as follows:
- (void)viewWillAppear:(BOOL)animated {
NSString *calcResult=[[NSString alloc] initWithFormat:@"%d",calcCount];
totalCalculations.text=calcResult;
[calcResult release];
[super viewWillAppear:animated];
}
Nothing here should be a surprise. We format a temporary string using the calcCount variable, set the totalCalcuations (UILabel) “text” property to the string, release the string, and pass the method invocation up the chain.
The code isn’t finished until the objects are released, so edit the dealloc method to release totalCalculations:
- (void)dealloc {
[totalCalculations release];
[super dealloc];
}
Congratulations! You just
completed a tab bar-based multi-view application with basic inter-view
communication! Your experience working with multiple views will open up a
whole new range of applications that you can develop.