Reputation: 47
So after a long trial and error the user in my App can now select a row from the first and root Viewcontroller1. When he does this a second Viewcontroller2 opens and replaces the first one on the screen. Wonderful I got this far, I used a NavigationController for this.
When the user selects a row I want the second Viewcontroller2 to display details about the selection. For example when you select a cow you get a picture of a cow + an explanation. When you select a horse the same set-up but with another picture and explanation.
Creating another viewcontroller for every animal seems bad practise. How do I solve this? I heard something about using views?
I know I will have to add some arrays and such because of the multiple rows but I will figure this out myself.
Thank you for reading this.
Upvotes: 1
Views: 70
Reputation: 8570
You're right on the edge of understanding
When the user selects a row I want the second Viewcontroller2 to display details about the selection.
Ok. Good. You have identified that you need a second view controller and that it should be responsible for displaying details.
For example when you select a cow you get a picture of a cow + an explanation. When you select a horse the same set-up but with another picture and explanation.
Excellent. You notice that each one has the same setup. And you also realise that repeating this work over and over again in separate view controllers is silly.
You need to keep the information about the animals separately, and structure them in a similar way. This is called a model. E.g. you might make an animal class that has a name property and a picture property.
@interface Animal : NSObject
@property (nonatomic, copy) NSString * name;
@property (nonatimic, strong) UIImage * picture;
@end
Then to display any animal you just have to make a view controller that knows how to take information from your model (your similarly structured pieces of data) and fill in the information in its view.
using our example we might see this code in a view controller
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
...
Animal * animalToDisplay = ...
self.imageView.image = animalToDisplay.picture;
self.nameLabel.text = animalToDisplay.name;
The animalToDisplay
object would be provided to this second controller by the controller before it, perhaps when tapping on a cell or button which corresponds to an animal. This view Controller can display the data from any Animal
object.
Upvotes: 1
Reputation: 5952
You are right that creating a different view for each animal would be bad practice. The general approach is to have a model with information on the animals and explanations. The view updates depending on which animal the user picks. That process is handled by a controller. This is called model-view-controller. The idea is that you have one layout for the animal details, but show different images and explanations depending on what animal is picked by having the controller update the labels using the model.
Apple explains navigating data using Table Views in the developer library. I suggest you also see the SimpleDrillDown sample code.
Upvotes: 1