programming4us
programming4us
MOBILE

iPhone Programming : Simplifying the Template Classes

- How To Install Windows Server 2012 On VirtualBox
- How To Bypass Torrent Connection Blocking By Your ISP
- How To Install Actual Facebook App On Kindle Fire
10/10/2010 5:26:17 PM
One of the annoying things about the Xcode templates is the long class names Xcode chooses for the default classes. While the default class names are OK for small programs, they can become somewhat unwieldy, and at times rather inappropriate, when the amount of code you have increases. So, we’re going to modify the template Xcode provides before we add our own code, using a process known as refactoring the code.

Why are we doing this refactoring? Well, later in the chapter we’re going to be using more than one view controller inside the project. The original name of the default view controller created by the Xcode template would be somewhat misleading. In addition to changing its name to something that reflects its purpose, we will shorten the CityGuideAppDelegate name.

Open the CityGuideAppDelegate.h file, right-click on the CityGuideAppDelegate class name in the interface declaration, and select Refactor, as shown in Figure 1. This will bring up the Refactoring window. Let’s change the name of the main application delegate class from CityGuideAppDelegate to CityGuideDelegate.

Figure 1. Select the class name, right-click, and select Refactor to access Xcode’s intelligent refactoring tool



Note: Since Objective-C does not have namespaces, it’s a common practice to prefix your class names with initials to avoid namespace collision, or the situation where two classes have the same name but do different things. For instance, the Apple classes have the prefix NS for historical reasons, as Cocoa was based on the NeXTSTEP frameworks.

Entering the new class name and clicking Preview, as I’ve done in Figure 2, shows us that three files will be affected by the change. Click Apply and Xcode will propagate the changes throughout the project. Remember to save the affected files before you refactor the next set of classes.

Figure 2. The Refactoring window



Note: If you find that the FileSave menu is grayed out in Xcode, click on the file you want to save and then click somewhere inside the file (it doesn’t matter where). Then you’ll be able to save the file.

You should also change the name of the CityGuideViewController class. Open the CityGuideViewController.h file and right-click on the CityGuideViewControllerCityGuideViewController to RootController. Entering the new class name and clicking Preview shows that this change is more extensive, with six files being affected by the change. Click Apply, and the changes will again propagate throughout the project. class name in the interface declaration, and again choose to refactor. Let’s change this class from

Notice, however, that Xcode has not changed the CityGuideViewController.xib file to be more appropriately named RootController.xib. We’ll have to make this change by hand. Click once on this file in the Groups & Files pane, wait a second, and click it again. You can then rename it to RootController.xib.

Unfortunately, since you had to make this change by hand, it hasn’t been propagated throughout the project. You’ll have to make some more manual changes. Double-click on the MainWindow.xib file to open it in Interface Builder. Click on the Root Controller icon in the main NIB window and open the Attribute pane of the Inspector window. As you can see in Figure 3, the NIB name associated with the root controller is still set as CityGuideViewController. Set this to RootController. You can either type the name of the controller into the window and Xcode will automatically perform name completion as you type, or use the control on the righthand side of the text entry box to get a drop-down panel where you’ll find the RootController class listed. Remember to save the NIB file using ⌘-S, and then test your refactoring by clicking the Build and Run (or depending on your Xcode setup, the Build and Debug) button in Xcode’s menu bar. You should see a bland gray screen pop up to prove that all is well.

Figure 3. Changing the NIB name from CityGuideViewController to RootController



Other  
 
programming4us
 
 
programming4us