Online Book Reader

Home Category

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

By Root 821 0
ScheduleFetcher uses its currentString, currentFields, and classes instance variables for this purpose. The string data that currentString accumulates for the current element is then stored in currentFields once the element ends. Once each class element ends, a ScheduledClass instance is created and populated with the values from currentFields, and the process starts over again for the next class.

Once the parsing is completed, a copy is made of the classes array (to make it immutable), and the result is returned.

Run the application again and use the debugger to inspect the classes array before it is returned to see that the XML is being parsed.

Lay Out the Interface


In RanchForecastAppDelegate.h, declare a table view outlet and an array of classes:

#import

@interface RanchForecastAppDelegate : NSObject

{

IBOutlet NSTableView *tableView;

NSArray *classes;

}

@property (strong) IBOutlet NSWindow *window;

@end

Open MainMenu.xib. Drop a table view onto the window. Ensure that the table view’s Content Mode is set to Cell Based. Set the table view to have three columns: Date, Class, and Location. Drag a date formatter onto the Date column cell and set its Date Style to Medium (Figure 28.5).

Figure 28.5. Lay Out the Interface

Control-click on the RanchForecastAppDelegate to bring up the Connection panel and connect the tableView outlet.

Control-click on the table view to bring up its Connection panel. Make RanchForecastAppDelegate the data source for the table view. (Don’t see the dataSource outlet? Did you select the scroll view instead of the table view?)

In Chapter 7, we talked about how powerful key-value coding can be. Let’s use that now to make things easy on the coding end and set the identitier of each table column to be the name of the ScheduledClass property we want to display in that column. Use the Identity Inspector for each column of the table view to set the identifiers to begin, name, and location for the Date, Class, and Location columns, respectively (Figure 28.6). If you are using a version of Xcode prior to 4.2, the identifer field will be shown in the Attributes Inspector. Use the Attributes Inspector to make each column not Editable.

Figure 28.6. Setting Identifier for Columns

Write Controller Code


Update RanchForecastAppDelegate.m to display the fetched classes in the table view:

#import "RanchForecastAppDelegate.h"

#import "ScheduleFetcher.h"

#import "ScheduledClass.h"

@implementation RanchForecastAppDelegate

@synthesize window;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification

{

ScheduleFetcher *fetcher = [[ScheduleFetcher alloc] init];

NSError *error;

classes = [fetcher fetchClassesWithError:&error];

[tableView reloadData];

}

Now add the NSTableViewDataSource methods:

#pragma mark -

#pragma mark NSTableViewDataSource

- (NSInteger)numberOfRowsInTableView:(NSTableView *)theTableView

{

return [classes count];

}

- (id)tableView:(NSTableView *)theTableView

objectValueForTableColumn:(NSTableColumn *)tableColumn

row:(NSInteger)row

{

ScheduledClass *c = [classes objectAtIndex:row];

return [c valueForKey:[tableColumn identifier]];

}

@end

Build and run the application. You should see a list of upcoming classes.

Opening URLs


The last step is to make it possible for the user to double-click on a class to open it in his or her browser.

In applicationDidFinishLaunching:, set the doubleAction and target of the table view:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification

{

[tableView setTarget:self];

[tableView setDoubleAction:@selector(openClass:)];

ScheduleFetcher *fetcher = [[ScheduleFetcher alloc] init];

NSError *error;

classes = [fetcher fetchClassesWithError:&error];

[tableView reloadData];

}

Finally, implement openClass:. Here you are using the NSWorkspace class. NSWorkspace represents the Finder.

- (void)openClass:(id)sender

{

ScheduledClass *c = [classes objectAtIndex:

[tableView clickedRow]];

Return Main Page Previous Page Next Page

®Online Book Reader