2012-02-16 50 views
3

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

我试着在IB中突出显示多个接口对象,并将连接拖到IBOutletCollection,但即使如此,它仍然只能一次挂钩它们。是否有一种隐藏的方式来同时连接多个?

谢谢

+0

我在这里同样的问题。这个问题很古老。苹果在此期间提出了解决方案吗?否则,我看不到收藏的努力。我可以(在我的情况下)使用父视图并使用子视图选择器。 – redestructa 2014-09-03 13:36:21

回答

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]]; 
} 
+0

谢谢。我已经向苹果提交了错误报告。 – beev 2012-02-17 14:00:42