IBOutletCollection - 一次连接多个对象

3
我一直在使用IBOutletCollections将相同的行为应用于在IB中连接的多个对象。这是一个很好的时间节省方法,但仍需要花费很长时间逐个在IB中连接每个对象和在我的头文件中声明的IBOutletCollection。
我尝试着在IB中高亮显示多个接口对象并拖动连接到IBOutletCollection上,但即使这样,它仍然只会一个一个地连接它们。有没有一种隐藏的方法可以一次连接多个对象呢?
谢谢

我也有同样的问题。这个问题很久了。苹果公司在此期间解决了这个问题吗?否则,我认为他们在集合方面没有付出努力。在我的情况下,我可以使用父视图并使用子视图选择器。 - redestructa
1个回答

2

是的...这比你想象的要困难。我建议在bugreporter.apple.com上使用雷达。

在我的代码中,我偶尔会采用像这样的代码方式。当我决定更改所有按钮的字体、背景颜色或其他内容时,它可以节省大量的时间、麻烦和错误。它具有IB的布局优势和代码的一致性。

// We have a lot of buttons that point to the same thing. It's a pain to wire
// them all in IB. Just find them all and write them up
- (void)wireButtons
{
  for (UIView *view in [self.view subviews])
  {
    if ([view isKindOfClass:[UIButton class]])
    {
      UIButton *button = (UIButton *)view;
      [button setTitle:[self buttonTitleForTag:button.tag] forState:UIControlStateNormal];
      button.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
      button.titleLabel.textAlignment = UITextAlignmentCenter;
      if (![button actionsForTarget:self forControlEvent:UIControlEventTouchUpInside])
      {
        [button addTarget:self action:@selector(performSomething:) forControlEvents:UIControlEventTouchUpInside];
      }
    }
  }
}

当我需要递归收集所有控件时(我用这个来实现弹出视图的穿透,但它也可以用于批量禁用)时,我使用类似的技术:

- (NSArray *)controlViewsForView:(UIView *)aView
{
  if (!aView)
  {
    return nil;
  }

  NSMutableArray *controlViews = [NSMutableArray new];
  for (UIView *subview in aView.subviews)
  {
    if ([subview isKindOfClass:[UIControl class]] && ! [self viewIsEffectivelyHidden:subview])
    {
      [controlViews addObject:subview];
    }
    [controlViews addObjectsFromArray:[self controlViewsForView:subview]];
  }

  return controlViews;
}

- (BOOL)viewIsEffectivelyHidden:(UIView *)view
{
  if (! view)
  {
    return NO;
  }
  if ([view isHidden])
  {
    return YES;
  }
  return [self viewIsEffectivelyHidden:[view superview]];
}

谢谢。我已经向苹果提交了错误报告。 - beev

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接