1

因此,经过长时间的试验和错误后,我的应用程序中的用户现在可以从第一个和根 Viewcontroller1 中选择一行。当他这样做时,第二个 Viewcontroller2 将打开并替换屏幕上的第一个。太好了,我走了这么远,为此我使用了 NavigationController。

当用户选择一行时,我希望第二个 Viewcontroller2 显示有关选择的详细信息。例如,当您选择一头奶牛时,您会得到一张奶牛的图片+一个解释。当您选择一匹马时,设置相同,但带有另一张图片和说明。

为每只动物创建另一个视图控制器似乎是不好的做法。我该如何解决这个问题?我听说了一些关于使用视图的事情?

我知道由于多行,我将不得不添加一些数组等,但我会自己解决这个问题。

谢谢您阅读此篇。

4

2 回答 2

1

您是对的,为每种动物创建不同的视图是不好的做法。一般的方法是建立一个模型,其中包含有关动物的信息和解释。视图会根据用户选择的动物进行更新。该过程由控制器处理。这称为模型视图控制器。这个想法是你有一个动物细节的布局,但通过让控制器使用模型更新标签,根据选择的动物显示不同的图像和解释。

Apple 解释了使用开发人员库中的表视图导航数据。我建议您也查看SimpleDrillDown 示例代码

于 2013-06-05T23:54:00.473 回答
1

你正处于理解的边缘

当用户选择一行时,我希望第二个 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.

于 2013-06-05T23:54:45.300 回答