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