【问题标题】:How to make lots of view controllers fast, in interface builder?如何在界面构建器中快速制作大量视图控制器?
【发布时间】:2011-08-03 19:18:38
【问题描述】:

我正在制作一个具有 tableview 和 DetailViewController 的应用程序。我使用 Apple 的“MultipleDetailViews”代码作为启动板。我想让这个应用程序在拆分视图中有大约 100 行,我希望每行的详细视图都发生变化(读取 100 个视图)。如何使用界面生成器做到这一点,但又不生成 100 个类文件并重复更改名称?

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

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

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

有什么省时的方法来做我要求的事情吗?

【问题讨论】:

    标签: iphone objective-c uitableview uiview uiviewcontroller


    【解决方案1】:

    100 个视图控制器类?这不好。

    单个视图控制器类的 100 个实例?我希望不会。

    让我们得到你用 2 描述的行为,只有 2。你有一个用于表视图的控制器和一个用于详细视图的控制器。就是这样。

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

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

    【讨论】:

      【解决方案2】:

      听起来您的每个详细视图都足够相似,您可以创建单个 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])];
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-10-06
        • 1970-01-01
        • 2018-08-04
        • 1970-01-01
        • 2017-10-20
        • 2018-06-06
        • 1970-01-01
        相关资源
        最近更新 更多