How to Add the Events Class in Your iOS App - dummies

How to Add the Events Class in Your iOS App

By Jesse Feiler

If Trip is to use an Events object in your iOS app, you had better create the class. This is a good example of how versatile programmers can be in app development. Follow these steps:

  1. In the Project navigator, select the Model Classes group and then either right-click the selection and choose New File from the menu that appears or choose File→New→File from the main menu (or press ⌘+N).

    Whatever method you choose, you’re greeted by the New File dialog.

  2. In the left column of the dialog, select Cocoa Touch under the iOS heading, select the Objective-C Class template in the top-right pane, and then click Next.

    You’ll see a dialog that will enable you to choose the options for your file.

  3. Enter Events in the Class field.

  4. Choose or enter NSObject in the Subclass Of field and then click Next.

    The iPad and With XIB for User Interface check boxes are dimmed because they are not relevant here — Events is derived from NSObject, and not from any type of view controller.

  5. In the Save sheet that appears, click Create.

The Events class is the model object that manages the events. You can imagine that the Events class could be expanded to do a lot more — such as return the location, process events from multiple sources, or even allow a user to add her own events.

To start adding the Events class, add the bolded code to Events.h.

@interface Events : NSObject
- (id)initWithDestinationIndex:
(NSUInteger)destinationIndex;
- (NSUInteger)numberOfEvents;
- (NSString *)getEvent:(NSUInteger)index;
–d

This code has three methods: an initialization method and two methods to process the Trip requests.

Next, you need to add a property. Add the code in bold to Events.m to create a class extension with a property. (The basic class extension without the property may already be in your project.)

#import "Events.h"
@interface Events ()
@property (strong, nonatomic) NSMutableArray *events;
–d
@implementation Events

As you can see, there is an initialization method (which is used by when it creates the object). Add the code to Events.m to implement the initWIthDestinationIndex: initialization method.

- (id)initWithDestinationIndex:
(NSUInteger)destinationIndex {
self = [super init];
if (self) {
NSString *filePath = [[NSBundle mainBundle]
pathForResource:@"Destinations" ofType:@"plist"];
NSDictionary *destinations = [NSDictionary
dictionaryWithContentsOfFile: filePath];
NSArray *destinationsArray =
destinations[@"DestinationData"];
NSDictionary *data =
destinationsArray[destinationIndex];
self.events = [NSMutableArray arrayWithArray:
data[@"Events"]];
}
return self;
}

All this method does at this point is get the array of URLs for the HTML pages you created and you entered in the Destinations plist. It puts these URLs in an array that you create — for more efficient retrieval later.

The EventsController, as you will see, will need to know the number of events and the event information. You’ve added the methods to Trip, but will actually be getting that information from Events. Add the code to Events.m to implement the method that returns the number of events.

- (NSUInteger)numberOfEvents {
return [self.events count];
}

To get the number of events, you return the count of the array.

The EventsController will also need to have a list of the event URLs. Add the code in Listing 16-10 to Events.m to implement that method.

- (NSString *)getEvent:(NSUInteger)index {
return self.events[index];
}

To return an Event , you return the URL based on the index into the array. This will make more sense when you go through the EventsController and EventPageController code.