我一直在使用IBOutletCollections将相同的行为应用到IB中连接的许多对象。这是一个很好的节省时间,但是在IB中的每个对象和我的头文件中声明的IBOutletCollection之间单独建立连接仍需要很长时间。
我已经尝试在IB中突出显示多个界面对象并将连接拖到IBOutletCollection,但即便如此,它仍然只能一次连接一个。是否有一种隐藏的方法可以同时连接多个人?
由于
答案 0 :(得分:2)
在我的代码中,我偶尔会在这样的代码中使用它。当我决定更改所有按钮的字体,或背景颜色等等时,它可以节省大量时间,麻烦和错误。它给出了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];
}
}
}
}
当我需要递归收集所有控件时,我使用类似的技术(我将此用于popover passthrough视图,但它对于大规模禁用也很有用):
- (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]];
}