Cocoa Programming for Mac OS X - Aaron Hillegass [116]
Chapter 30. Developing for iOS
Applications for the iPhone and iPad are written using Xcode and the Cocoa Touch Frameworks. Cocoa Touch comprises Foundation, Core Graphics, and UIKit. UIKit is analogous to AppKit, supplying the windows, events, views, buttons, and so on, for iPhone programmers. UIKit is, however, not the same as AppKit. This chapter will get you started developing applications on iOS, with an emphasis on what is not the same.
In particular, you will not have the garbage collector, but you can use ARC on iOS 5 or, if you choose, manual reference counting (retain/release) for memory management. You will use OpenGL ES instead of regular OpenGL. Windows and table-view cells are subclasses of UIView.
Porting RanchForecast to iOS
Most of the stuff that makes RanchForecast work (table views, NSXMLParser, NSURLConnection) exists on iOS. Porting it all from Cocoa to Cocoa Touch will give you a feel for many of the differences between the two platforms. You will use two common iOS features in your port: a navigation controller and a table view. There will be two view controllers (subclasses of UIViewController) (Figure 30.1).
Figure 30.1. RootViewController and ScheduleViewController
View controllers are just what they sound like: controller classes that are responsible for a UIView and its contents. In iOS view controllers are generally used to represent one screen of information. We will discuss the role of view controllers in AppKit in the next chapter.
The navigation controller manages a stack of view controllers, animating them on and off the screen. The navigation bar, at the top, provides users with a sense of their place within the stack, usually with a Back button on the left, a title in the center, and sometimes a button on the right. An iOS app has only one window. View controllers are used extensively in conjunction with navigation controllers to create the sense of multiple screens.
In Xcode, create a new iOS Application using the Empty Application template (Figure 30.2). Some versions of Xcode refer to this as a Window-Based Application.
Figure 30.2. New iOS Empty Application
Name the project RanchForecastTouch, set the Class Prefix to RanchForecastTouch, and set Device Family to Universal (Figure 30.3). This will create more files, but it will be easier in the future to tailor your app for both iPhone and iPad devices.
Figure 30.3. Name the New Project
ScheduleFetcher
You are going to use the same ScheduleFetcher class and ScheduledClass model that you wrote in Chapter 29. Drag ScheduleFetcher.h, ScheduleFetcher.m, ScheduledClass.h, and ScheduledClass.m into this project. Be sure to check the Copy items into destination group’s folder check box (Figure 30.4).
Figure 30.4. Copy Files
RootViewController
Create a new UIViewController subclass called RootViewController. Check the option box labeled With XIB for user interface (Figure 30.5).
Figure 30.5. New UIViewController Subclass
Open RootViewController.xib. In the Attributes Inspector for the view, under Simulated Metrics, set Top Bar to Navigation Bar (Figure 30.6). This sets the size of the view to be appropriate for a navigation bar.
Figure 30.6. Simulated Navigation Bar
Drag a button and activity indicator onto the view. Set the button’s title to Fetch Classes, and set the activity indicator to Hides When Stopped (Figure 30.7).
Figure 30.7. Set the Activity Indicator to Hides When Stopped
Now enable the Assistant Editor. This will simplify creating the outlets and actions that we need. Ensure that the Assistant Editor’s jump bar is set to Automatic; this will display RootViewController.h in the editor.
Control-drag from the button to the RootViewController interface declaration in the Assistant Editor and create an outlt called fetchButton. See the Storage to Weak (Figure 30.8).
Figure 30.8. Control-Drag to Create the fetchButton Outlet
Repeat the process for