繁体   English   中英

如何在界面生成器中快速制作许多视图控制器?

[英]How to make lots of view controllers fast, in interface builder?

我正在制作一个具有表视图和DetailViewController的应用程序。 我正在使用Apple的“ MultipleDetailViews”代码作为启动板。 我想使该应用在拆分视图中具有约100行,并且我希望每行更改详细信息视图(读取100个视图)。 如何使用界面生成器执行此操作,而又不生成100个类文件并重复更改名称?

目前,我唯一的方法是手动创建单独的视图控制器(带有类文件)。 但是,这非常令人激动。

无论如何,我可以使用一个DetailViewController,在接口构建器中向其中添加几个视图,并在tableview中选择一行时推送每个视图。

在每个视图上,我想添加一个背景图像和三个包含不同声音的按钮(每行的视图将具有三种独特的声音)。 如何仅创建三个IBAction并根据选择的行更改声音文件路径?

有什么省时的方法可以完成我要问的事情吗?

100个视图控制器类? 不好

一个视图控制器类的100个实例? 我不希望这样。

让我们了解一下用2来描述的行为。您有一个用于表视图的控制器和一个用于详细视图的控制器。 而已。

当您在表视图中选择一行时,将该行索引传递给详细视图控制器,并为详细视图控制器提供一种基于该行加载正确的图像和声音的方法。

这可能来自图像和声音资源的命名约定(“ background0”,“ background1”,...),也可能来自某个配置文件,该文件定义了背景图像和每一行的声音(包含字典数组:[{background:“ moon.png”,firstSound:“ clown.mp3”,secondSound:“ moose.mp3”,thirdSound:“ water.mp3},{...},...])。

听起来每个细节视图都足够相似,您可以创建一个UIViewController实例,并在每次点击一个单元格时重新配置它。 这是一个示例,说明如何更改MultipleDetailViews项目以根据选择的行来更改单个UIViewController实例的背景色。

static const NSUInteger kRowCount = 100;

- (void)viewDidLoad {
    [super viewDidLoad];
    self.contentSizeForViewInPopover = CGSizeMake(310.0, self.tableView.rowHeight*kRowCount);
    // Create an array of colors to cycle through
    self.colors = [NSArray arrayWithObjects:[UIColor redColor], [UIColor greenColor], [UIColor blueColor], nil];
}

#pramga mark - UITableViewDataSource methods

- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section {
    return kRowCount;
}

- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // Dequeue or create cell
    cell.textLabel.text = [NSString stringWithFormat:@"View Controller #%d", indexPath.row + 1];
    return cell;
}

#pramga mark - UITableViewDataDelegate methods

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Don't create a new view controller, just reconfigure the on that is already displayed
    DetailViewController *dvc = [self.splitViewController.viewControllers objectAtIndex:1];
    dvc.view.backgroundColor = [colors objectAtIndex:(indexPath.row % [colors count])];
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM