Online Book Reader

Home Category

Cocoa Programming for Mac OS X - Aaron Hillegass [117]

By Root 817 0
the activity indicator: Create an outlet called activityIndicator. Then Control-drag from the button to the properties section of RootViewController and create an action called fetchClasses:. Note that the result of this process is the same as typing out the outlets and actions manually, then Control-dragging between Connection panels and the interface objects. The same can be done in Mac and iOS projects alike.

Add a Navigation Controller


As our application stands, it will show a blank view on launching. We want the app to start out with the RootViewController, which has the Fetch Classes button we just wired up. Furthermore, when the user touches Fetch Classes, the application should display a list of classes. As we mentioned earlier in the chapter, iOS’s navigation controller provides an easy way to manage the display of our view controllers.

Open RanchForecastTouchAppDelegate.h and add an instance variable for the navigation controller:

#import

@interface RanchForecastTouchAppDelegate : NSObject

{

UINavigationController *navController;

}

@property (nonatomic, strong) IBOutlet UIWindow *window;

@end

The root-view controller is the lowest view controller on the stack and generally the first view controller that the user will see. In RanchForecastTouchAppDelegate.m, instantiate the navigation controller as well as the root-view controller:

#import "RanchForecastTouchAppDelegate.h"

#import "RootViewController.h"

@implementation RanchForecastTouchAppDelegate

@synthesize window=_window;

- (BOOL)application:(UIApplication *)application

didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

RootViewController *rvc;

rvc = [[RootViewController alloc] initWithNibName:nil bundle:nil];

navController = [[UINavigationController alloc]

initWithRootViewController:rvc];

self.window = [[UIWindow alloc]

initWithFrame:[[UIScreen mainScreen] bounds]];

[self.window addSubview:[navController view]];

[self.window makeKeyAndVisible];

return YES;

}

Note that iOS has a UIApplicationDelegate method, application:didFinishLaunchingWithOptions:, which is very similar to Cocoa’s applicationDidFinishLaunching:.

Build and run the application. It will launch in the simulator and you should see the Fetch Classes button. You can choose between launching your application in the iPhone or iPad simulator using the Scheme popup on the Xcode toolbar.

ScheduleViewController


The second view controller, ScheduleViewController, will be used to display the schedule itself once the fetch has completed. Create a new UIViewController Subclass, but this time set it to be a subclass of UITableViewController. Uncheck With XIB for user interface. Name it ScheduleViewController. UITableViewController is itself a subclass of UIViewController.

Open ScheduleViewController.h and add two instance variables and a property:

#import

@interface ScheduleViewController : UITableViewController {

NSArray *classes;

NSDateFormatter *dateFormatter;

}

@property (nonatomic, strong) NSArray *classes;

@end

In ScheduleViewController.m, import ScheduledClass.h, synthesize classes and make some additions to initWithStyle: and dealloc:

#import "ScheduleViewController.h"

#import "ScheduledClass.h"

@implementation ScheduleViewController

@synthesize classes;

- (id)initWithStyle:(UITableViewStyle)style

{

self = [super initWithStyle:style];

if (self)

{

dateFormatter = [[NSDateFormatter alloc] init];

[dateFormatter setDateStyle:NSDateFormatterLongStyle];

[[self navigationItem] setTitle:@"Schedule"];

}

return self;

}

Note that we are setting the title of this view controller’s navigation item. Each view controller has a navigation item, which determines what is displayed in the navigation bar when that view controller is topmost. The title is shown in the navigation bar; it is also used to provide the title for the Back button when the topmost view controller is directly above this one. It is also possible to configure

Return Main Page Previous Page Next Page

®Online Book Reader