简体   繁体   English

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

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

I'm making an app which has a tableview and a DetailViewController. 我正在制作一个具有表视图和DetailViewController的应用程序。 I'm using Apple's "MultipleDetailViews" code as a launchpad. 我正在使用Apple的“ MultipleDetailViews”代码作为启动板。 I want to make this app have around 100 rows in the split view and I want the detail view to change for each row (read 100 views). 我想使该应用在拆分视图中具有约100行,并且我希望每行更改详细信息视图(读取100个视图)。 How can I do this using interface builder but without generating 100 class files and changing names repeatedly? 如何使用界面生成器执行此操作,而又不生成100个类文件并重复更改名称?

Currently the only method I have is to create separate view controllers (with class files) manually. 目前,我唯一的方法是手动创建单独的视图控制器(带有类文件)。 This is very agitating, however. 但是,这非常令人激动。

Is there anyway I can use one DetailViewController, add several views to it inside interface builder and push each view when I choose a row in the tableview. 无论如何,我可以使用一个DetailViewController,在接口构建器中向其中添加几个视图,并在tableview中选择一行时推送每个视图。

On each view I want to add a background image and three buttons that contain different sounds (every row's view will have three unique sounds). 在每个视图上,我想添加一个背景图像和三个包含不同声音的按钮(每行的视图将具有三种独特的声音)。 How can I only create three IBActions and change the sound file path according to what row is selected? 如何仅创建三个IBAction并根据选择的行更改声音文件路径?

Is there any time-efficient way to do what I am asking? 有什么省时的方法可以完成我要问的事情吗?

100 view controllers classes? 100个视图控制器类? That's no good. 不好

100 instances of a single view controller class? 一个视图控制器类的100个实例? I should hope not. 我不希望这样。

Let's get you the behavior you described with 2, just 2. You have a controller for your table view and a controller for your detail view. 让我们了解一下用2来描述的行为。您有一个用于表视图的控制器和一个用于详细视图的控制器。 That's it. 而已。

When you select a row in the table view pass that row index to the detail view controller and give the detail view controller a way to load the correct image and sounds based on that row. 当您在表视图中选择一行时,将该行索引传递给详细视图控制器,并为详细视图控制器提供一种基于该行加载正确的图像和声音的方法。

That could be from a naming convention for your image and sound resources ('background0', 'background1', ...) or it could be from some config file which defines the the background image and sounds for each row (a plist containing an array of dictionaries: [{background:"moon.png", firstSound:"clown.mp3", secondSound:"moose.mp3", thirdSound:"water.mp3}, {...}, ...]). 这可能来自图像和声音资源的命名约定(“ background0”,“ background1”,...),也可能来自某个配置文件,该文件定义了背景图像和每一行的声音(包含字典数组:[{background:“ moon.png”,firstSound:“ clown.mp3”,secondSound:“ moose.mp3”,thirdSound:“ water.mp3},{...},...])。

It sounds like each of your detail views will be similar enough that you can create a single UIViewController instance and reconfigure it each time a cell is tapped. 听起来每个细节视图都足够相似,您可以创建一个UIViewController实例,并在每次点击一个单元格时重新配置它。 Here is an example of how you might alter the MultipleDetailViews project to change the background color of a single UIViewController instance depending on which row is selected. 这是一个示例,说明如何更改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