你正处于理解的边缘
当用户选择一行时,我希望第二个 Viewcontroller2 显示有关选择的详细信息。
行。好的。您已经确定需要第二个视图控制器并且它应该负责显示详细信息。
例如,当您选择一头奶牛时,您会得到一张奶牛的图片+一个解释。当您选择一匹马时,相同的设置但带有另一张图片和说明。
优秀的。您注意到每个都有相同的设置。而且您还意识到在单独的视图控制器中一遍又一遍地重复这项工作是愚蠢的。
您需要单独保存有关动物的信息,并以类似的方式构建它们。这称为模型。例如,您可以创建一个具有 name 属性和 picture 属性的动物类。
@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.