MOBILE

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

3/19/2011 4:38:47 PM

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.

Figure 10. Add one label for the output value and one to serve as a description.


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.

Figure 11. Connect the summary view controller to the area and volume view controllers.

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.

Other  
  •  iPhone Application Development : Building a Multi-View Tab Bar Application (part 3) - Implementing the Volume View
  •  iPhone Application Development : Building a Multi-View Tab Bar Application (part 2) - Implementing the Area View
  •  iPhone Application Development : Building a Multi-View Tab Bar Application (part 1)
  •  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)
  •  
    Top 10
    Cocoa Fundamentals
    MySQL Server Monitoring (part 1) - SQL Commands
    Programming .NET Security : Programming Cryptographic Keys (part 3) - Key Exchange Formatting
    Configure Server Core Postinstallation
    iPhone 3D Programming : Holodeck Sample (part 4) - Replacing Buttons with Orientation Sensors
    Monitoring a SharePoint 2010 Environment : Using SharePoint’s Native Reporting Capabilities
    Windows System Programming : Registry Management
    Safeguarding Confidential Data in SharePoint 2010 : Enabling SQL Database Mirroring
    Windows Server 2008 : DHCP/WINS/Domain Controllers - Installing and Configuring WINS
    iPhone 3D Programming : Vector Beautification with C++
    Most View
    Exchange Server 2010 : Deploying a Database Availability Group (part 2) - Suspending and Reseeding a Database
    BizTalk 2006 : Building a Resequencing Aggregator
    Permissions: Extending the .NET Framework
    Integrating Office Communications Server 2007 in an Exchange Server 2010 Environment - Understanding Microsoft’s Unified Communications Strategy
    Watch for File System Changes
    Security Policy Explained in .NET
    Exchange Server 2010 : Installing OCS 2007 R2 (part 3) - Configuring Prerequisites & Deploying an OCS 2007 Server
    Security Center in Windows Vista
    Windows 7 : Using Windows Live Contacts
    Windows Server 2008 : The Migration Planning Phase - Documenting the Process for Migration
    Mobile Application Security : WebOS Security - Introduction to the Platform
    Windows Server 2008 R2 Active Directory Domain Services Primer : Outlining the Role of DNS in AD DS
    SharePoint 2010 : Operations Management with the SharePoint Central Administration Tool (part 4) - Reviewing Backup and Restore Settings in SPCA
    Examining Real-World SharePoint 2010 Deployments
    Mobile Application Security : BlackBerry Security - Networking
    SharePoint 2010 : Business Intelligence - Visio Services
    Sharepoint 2007: Upload a File from an Office Application
    Becoming an Excel Programmer : View Results
    OData with SQL Azure - OData Overview
    Programming the Mobile Web : Multimedia and Streaming