iPhone Application Development : Creating a Multi-View Toolbar Application (part 1)

2/28/2011 10:58:57 AM
The first project that we’ll be building in this article will create and manage multiple views from scratch. This will serve as an exercise to familiarize you with an approach to dealing with multiple views and introduce you to some new methods and properties, as well as the toolbar interface element.

Implementation Overview

Much as a single-view application uses a view controller to direct the interactions of its interface elements, a multi-view application needs a controller to help it switch between different views. In this implementation, we’ll be creating a typical view-based application but will be using the default view controller (UIViewController) to swap in and out three additional views—each with its own controller. The default controller will need to implement methods for clearing the current view and loading any of the other views—in any order, at any time.

In addition, our default view will be setting up a toolbar. We’ll use the toolbar to provide quick access to view switching via a row of buttons at the bottom of the screen, as shown in Figure 1.

Figure 1. The final application will switch between views with a toolbar.

By the Way

One interesting challenge that we need to overcome with this implementation is that while our main view will include the toolbar, it must be visible in all of the other views as well. We’ll need to display the views “under” the toolbar so that it isn’t hidden as new views are shown or removed.

Setting Up the Project

To begin, start Xcode and create a new application called MultipleViews using the View-Based Application template. The layout of this template should be getting very familiar by now. The MainWindow.xib includes a view controller that loads its view from a second XIB file, MultipleViewsViewController.xib. This XIB file will hold the toolbar that serves as our interface for switching between views, as well as instances of three new view controllers that will ultimately manage three new views.

Adding Views and View Controllers

Each of the views that we will be switching between needs its own view controller and XIB file. Create three new UIViewController subclasses (File, New File, Cocoa Touch Class, UIViewController subclass) and name them as follows:

  • FirstViewController

  • SecondViewController

  • ThirdViewController

Be sure to check the With XIB for User Interface checkbox for each of the new classes that you create. After you’ve created the files, drag the XIB files to the Resources group. Your Xcode Classes and Resources groups should resemble Figure 2.

Figure 2. Our first multi-view application will use a total of four views and view controllers.

By the Way

The goal of this project is to create a simple application that switches between independently controlled views. It is certainly possible that each of the views could share a view controller, but structurally it is a good practice to use separate controllers for views that do not serve the same function.

Prepping the View Content

To make sure that we know (visually) which view is which, open each of the three XIB files that correspond to the view controller classes in Interface Builder. For each view, drag a text label (UILabel) from the Library (Tools, Library) into the view. Change the text of the label to 1 in FirstViewController.xib, 2 in SecondViewController.xib, and 3 in the ThirdViewController.xib file. In the sample files, for this project, we’ve also set the font size to 288 points for each label by using the Attributes Inspector (Command+1), as shown in Figure 3.

Figure 3. Add labels to each view so they can be easily identified.

Remember that we’re going to be overlaying a toolbar on each of these views, so you don’t have the entire screen real estate to work with. Although we can’t see the toolbar just yet, we can simulate it for the purposes of laying out the view. To add a simulated toolbar to the view, select the view itself within the Interface Builder Document window and then press Command+1 to open the View Attributes Inspector. Choose Toolbar from the Bottom Bar Simulated User Interface Elements pop-up menu, demonstrated in Figure 4.

Figure 4. Adding a simulated toolbar to the view can help with layout since the actual toolbar isn’t visible.
  •  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
  •  iPhone Application Development : Making Multivalue Choices with Pickers - Using Date Pickers (part 1)
  •  iPhone Application Development : Making Multivalue Choices with Pickers - Understanding Pickers
  •  Sync Your iPad with iTunes : Troubleshooting iTunes and the Sync
  •  Sync Your iPad with iTunes : Manually Transferring Music, Movies, Podcasts, and More on Your iPad (Drag-and-Drop Method)
  •  Windows Phone 7 Development : Internationalization - Using Resource Files to Localize Content
  •  Windows Phone 7 Development : Internationalization - Storing and Retrieving Current Culture Settings
  •  Mobile Application Security : WebOS Security - Development and Security Testing
    Top 10
    Canon EOS 6D - A Beginner-Friendly Full-Frame Camera
    Fujifilm FinePix F800EXR - A Versatile Camera With Wireless Connectivity
    8 Essential Free Apps – Q1 2013
    Your First Steps - Start Your Windows Journey Here
    Asus Zenbook Prime UX31A Touch Review - New Touchscreen And Equally Stable Performance (Part 2)
    Asus Zenbook Prime UX31A Touch Review - New Touchscreen And Equally Stable Performance (Part 1)
    Dell XPS 13 - Look, Don't Touch
    HTC Butterfly - Transform The Way You See And Hear The World
    HTC Desire SV Review – A Shining Mid-Range Dual-SIM Smartphone
    Lenovo K860 - Android Smartphone
    Most View
    Our Essential Guide To Camera Triggers
    The Transformers (Part 3) : HP Envy X2, Acer Aspire S7, Dell Latitude 10
    Samsung Series 5 550 Chromebook – A real “paperweight” ?
    Apple Society – Less Never More
    Designing and Implementing Mobility in Exchange Server 2010: Securing Access to ActiveSync with Secure Sockets Layer Encryption
    Programming Windows Services with Microsoft Visual Basic 2008 : Administration of Services
    Customizing the Windows Vista Interface : Customizing the Start Menu for Easier Program and Document Launching (part 1)
    The Big Story – Take TV Anywhere
    Windows Server 2008 R2 Active Directory Domain Services Primer : Understanding Domain Trusts
    iPhone Application Development : Reading and Writing User Defaults (part 2) - Implementing System Settings
    Security - The Human Element (Part 1)
    Mesh Elite 3450-W8CS - Amazing Pack Of Power
    Microsoft Visual Basic 2008 : Processing and Storing Data in SQL Server 2005 - Optimizing the LINQSQL Class
    Ditch Your Laptop For Your Phone (Part 5)
    Programming .NET Security : Symmetric Encryption Explained (part 1) - Creating the Encrypted Data
    Unifying: Greatest Challenge
    USB Hard Drive Hangout (Part 2) - Toshiba Canvio plus 3.0 1.5TB, Adata Dashdrive Elite 500GB HE720
    You Can Master RAW (Part 4)
    Get This Machine’s Hostname and IP Address
    Corel PaintShop Pro X4