Editing Attractions

The task of getting Fort Collins for Kids to edit Attractions in the AttractionDetailView has been harder than expected. I got some boilerplate code from The Big Nerd Ranch, but that code doesn’t provide the Undo button that I want in this app.

There were a number of different ways to do this- I chose to put almost all of the logic into the AttractionDetailView itself. The interface looks like this:

- (id)initWithExistingAttraction:(Attraction *)a;
- (id)initWithNewAttraction;

Inside I keep around two private properties, attraction and existingAttraction as well as a boolean named usingExistingAttraction.

Here’s the logic for initWithExistingAttraction:

usingExistingAttraction = YES;
[self setAttraction:[a copy]];
[self setExistingAttraction:a];

It’s probably worth pointing out that I changed the Attraction class to support NSCopying so that I can do a true deep copy.

Here’s the logic for initWithNewAttraction:

usingExistingAttraction = NO;
[self setAttraction:[[Attraction alloc] init]];

This creates a blank attraction and starts editing on it.

The cancel routine just dismisses the form, but the save routine does some interesting work:

if (usingExistingAttraction) {
    [[AttractionStore defaultStore] replaceAttraction:_existingAttraction withAttraction:_attraction];
} else {
    [[AttractionStore defaultStore] addAttraction:_attraction];
}

If we’re editing an existing attraction, we replace the current attraction in the global data store with the new one (and yes, the replaceAttraction:withAttraction routine is a new one crafted for this purpose.) If it’s a new attraction we’re editing, we just add it to the global data store.

It works.

But I don’t like it. There are two things I don’t like about it:

  • The detail view controller needs to know whether or not it’s editing a new attraction or an existing attraction
  • The detail view controller needs to know about the AttractionStore

As I was having a beer with my friend Paul it occured to me that setting up a delegate would be a cleaner design. Now, the existing design isn’t that bad, and I could probably live with it. But the point of this project is to learn about iOS development, and I’ve never defined my own delegate, so… time to get to work!

One Reply to “Editing Attractions”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.