3

I've been using IBOutletCollections to apply the same behaviour to many objects which are hooked up in IB. This is a great time saver, but it still takes a long time to individually make the connection between each object in IB and the IBOutletCollection declared in my header file.

I've tried highlighting multiple interface objects in IB and dragging a connection to the IBOutletCollection, but even so it still only hooks them up one at a time. Is there a hidden way to connect many at once?

Thanks

beev
  • 1,197
  • 2
  • 16
  • 33
  • I'm here with the same question. The question is old. Did apple bring up a solution in the meantime? Otherwise I don't see an effort with collections. I can (in my case) use parent views and use the subviews selector. – redestructa Sep 03 '14 at 13:36

1 Answers1

2

Yeah... it is harder than you'd think. I recommend a radar at bugreporter.apple.com.

In my code, I've occasionally resorted to doing it in code like this. It saves a lot of time, hassle and bugs when I decide to change the font for all the buttons, or the background color or whatever. It gives the layout advantages of IB with the consistency of code.

// 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];
      }
    }
  }
}

I use a similar technique when I need to recursively collect all the controls (I use this for popover passthrough views, but it's also useful for mass disable):

- (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]];
}
Rob Napier
  • 286,113
  • 34
  • 456
  • 610