IBOutletCollection - 一次连接多个对象

发布于 2025-01-05 20:49:07 字数 234 浏览 0 评论 0原文

我一直在使用 IBOutletCollections 将相同的行为应用于 IB 中连接的许多对象。这是一个很好的节省时间的方法,但是单独建立 IB 中的每个对象和我的头文件中声明的 IBOutletCollection 之间的连接仍然需要很长时间。

我尝试在 IB 中突出显示多个接口对象并将连接拖动到 IBOutletCollection,但即使如此,它仍然一次只连接一个。有没有一种隐藏的方式可以同时连接多个人?

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时间你老了 2025-01-12 20:49:07

是的...这比你想象的要难。我在 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]];
}

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]];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文