Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ethical

macrumors 68000
Original poster
Dec 22, 2007
1,661
1
Hi guys,

Hopefully I can explain this properly...

I'm making a cookery application, and I have a view that lists the recipes, then on the next view (after selecting the recipe) is a view with a picture of the final product, some info about the recipe (time, difficulty, cost etc), and then there is a table view that will have the instructions on it.

I don't have separate view controllers for each recipe page, I just have one. Can I create an array for each recipe, and then whichever recipe is selected, the corresponding array gets put onto the table view, same thing with the images etc. Is this the easiest way to do it? Does this even make sense? Hopefully it does.

Thanks!

p.s. in case you didn't realise, I'm a complete noob. apologies in advance if i say (or have said!) something stupid.
 
Yes, this is a very good design pattern. If you have five different recipes, five different view controllers might not be too hard to handle. As soon as you have fifty recipes, fifty view controllers becomes impractical. You want to create one class that you can reuse again and again.

Usually, when I have a pattern like this, the Root View Controller holds an NSArray "model". This is an array of dictionaries. When you push to a detail controller, the detail controller fills the information from the appropriate dictionary. There are a couple of different ways to do this. One way is to take the appropriate recipe dictionary and assign it to the detail controller. I prefer delegation: the detail controller calls back to the root controller whenever it needs data from the model.

You might also want to look into Core Data (which can be more efficient than an XML array) to manage the model. There is even Apple sample code called "iPhoneCoreDataRecipes". Have you seen anything good there?
 
Thanks for the reply. This seems a lot more complicated than I imagined, and a little out of my depth at the moment, so I'll have to re-think some things. Thanks again!
 
Ok, I've started reading into Core Data, but it'll be a while before I'll feel comfortable implementing this. In the meantime, just because I want to experiment, would this work ok... Create a .txt file for each of the recipies, then when the user goes to the RecipeDetail view, a switch statement determines which of the .txt files are loaded on a textView on the page? I realise this may be a little impractical if there are lots of recipes, but will it work? Thanks!
 
Ok, I've started reading into Core Data, but it'll be a while before I'll feel comfortable implementing this. In the meantime, just because I want to experiment, would this work ok... Create a .txt file for each of the recipies, then when the user goes to the RecipeDetail view, a switch statement determines which of the .txt files are loaded on a textView on the page? I realise this may be a little impractical if there are lots of recipes, but will it work? Thanks!

Yup, that would work, impractical as you say, but it would certainly work.

Instead of a switch statement though you could save an NSDictionary to a plist file, in the NSDictionary you could have key/values like Recipe name => Recipe filename. That would make it a bit more manageble than a switch statement, scaling wise.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.