In this section, we present a working
iPhone application based on the code developed so far. The application
will present the contents of an RSS feed to the user in a scrollable
table view. The complete application can be found in the RSS Reader project in the source downloads.
Listing 1 shows the application delegate declaration and definition. The application delegate uses an instance of the class XORSSFeedNebraska to retrieve the XML document and parse it to generate the items.
Example 1. The RSS reader application delegate declaration and definition.
#import <UIKit/UIKit.h> #import "XORSSFeedNebraska.h" @interface XORSSFeedAppDelegate : NSObject { UIWindow *window; UINavigationController *navigationController; XORSSFeedNebraska *rssFeed; } @property (nonatomic, retain) UINavigationController *navigationController; @end
#import "XORSSFeedAppDelegate.h" #import "XORSSFeedNebraska.h" #import "RootViewController.h"
@implementation XORSSFeedAppDelegate @synthesize navigationController; - (void)applicationDidFinishLaunching:(UIApplication *)application { window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Create the navigation and view controllers RootViewController *rootViewController = [[RootViewController alloc] init]; UINavigationController *aNavigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
self .navigationController = aNavigationController; [aNavigationController release]; [rootViewController release]; // Configure and show the window [window addSubview:[navigationController view]]; rssFeed = [[XORSSFeedNebraska alloc] init]; [rssFeed fetchAbsconders]; [window makeKeyAndVisible]; } -(NSString*)xoTitle{ return @"Nebraska Absconders"; } - (NSInteger)countOfList { return [rssFeed numberOfAbsconders]; } - (id )objectInListAtIndex:(NSUInteger)theIndex { return [rssFeed absconderAtIndex:theIndex]; } - (void )dealloc { [window release]; [navigationController release]; [rssFeed release]; [super dealloc]; } @end
|
The delegate's applicationDidFinishLaunching: method creates a window and the table view controller. It then asks the instance of XORSSFeedNebraska to fetch the XML and parse it by calling the fetchAbsconders method. The table view controller's declaration and definition are shown in Listing 2.
Example 2. The RSS reader table controller declaration and definition.
#import <UIKit/UIKit.h> @interface RootViewController : UITableViewController { } @end
#import "RootViewController.h" #import "XOAbsconder.h" #import "XORSSFeedAppDelegate.h" @implementation RootViewController - init { if (self = [super init]) { XORSSFeedAppDelegate *appDelegate = (XORSSFeedAppDelegate*)[[UIApplication sharedApplication] delegate]; self.title = [appDelegate xoTitle]; }
return self; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { XORSSFeedAppDelegate *appDelegate = (XORSSFeedAppDelegate*)[[UIApplication sharedApplication] delegate]; return [appDelegate countOfList]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XO"]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"XO"] autorelease]; } XORSSFeedAppDelegate *appDelegate = (XORSSFeedAppDelegate*)[[UIApplication sharedApplication] delegate]; XOAbsconder *absconder = [appDelegate objectInListAtIndex:indexPath.row]; cell.textLabel.text = [absconder description]; return cell; } @end
|