0

我在情节提要上有 3 种控件类型(bigButton0....bigButton11、btnSmall0...btnSmall11 和 lbl0...lbl11)。

我想将相同的图像分配给所有 btnBig0 到 btnBig11。我目前正在使用冗长的冗余代码来执行此操作,因为我没有找到任何更智能的解决方案,但我需要在运行时执行此操作。

有人可以告诉我如何缩短我的代码吗?非常感谢你。

NSString * sImageName = [NSString stringWithFormat:@"button_bg_%.0f_%.0f_normal.png", L_CELL_W, L_CELL_H];

[bigButton0 setImage:[UIImage imageNamed:sImageName] forState:UIControlStateNormal];
[bigButton1 setImage:[UIImage imageNamed:sImageName] forState:UIControlStateNormal];
[bigButton2 setImage:[UIImage imageNamed:sImageName] forState:UIControlStateNormal];
[bigButton3 setImage:[UIImage imageNamed:sImageName] forState:UIControlStateNormal];
[bigButton4 setImage:[UIImage imageNamed:sImageName] forState:UIControlStateNormal];
[bigButton5 setImage:[UIImage imageNamed:sImageName] forState:UIControlStateNormal];
[bigButton6 setImage:[UIImage imageNamed:sImageName] forState:UIControlStateNormal];
[bigButton7 setImage:[UIImage imageNamed:sImageName] forState:UIControlStateNormal];
[bigButton8 setImage:[UIImage imageNamed:sImageName] forState:UIControlStateNormal];
[bigButton9 setImage:[UIImage imageNamed:sImageName] forState:UIControlStateNormal];
[bigButton10 setImage:[UIImage imageNamed:sImageName] forState:UIControlStateNormal];
[bigButton11 setImage:[UIImage imageNamed:sImageName] forState:UIControlStateNormal];

sImageName = [NSString stringWithFormat:@"button_bg_%.0f_%.0f_down.png", L_CELL_W, L_CELL_H];

[bigButton0 setImage:[UIImage imageNamed:sImageName] forState:UIControlStateHighlighted];
[bigButton1 setImage:[UIImage imageNamed:sImageName] forState:UIControlStateHighlighted];
[bigButton2 setImage:[UIImage imageNamed:sImageName] forState:UIControlStateHighlighted];
[bigButton3 setImage:[UIImage imageNamed:sImageName] forState:UIControlStateHighlighted];
[bigButton4 setImage:[UIImage imageNamed:sImageName] forState:UIControlStateHighlighted];
[bigButton5 setImage:[UIImage imageNamed:sImageName] forState:UIControlStateHighlighted];
[bigButton6 setImage:[UIImage imageNamed:sImageName] forState:UIControlStateHighlighted];
[bigButton7 setImage:[UIImage imageNamed:sImageName] forState:UIControlStateHighlighted];
[bigButton8 setImage:[UIImage imageNamed:sImageName] forState:UIControlStateHighlighted];
[bigButton9 setImage:[UIImage imageNamed:sImageName] forState:UIControlStateHighlighted];
[bigButton10 setImage:[UIImage imageNamed:sImageName] forState:UIControlStateHighlighted];
[bigButton11 setImage:[UIImage imageNamed:sImageName] forState:UIControlStateHighlighted];
4

1 回答 1

3

有多种方法可以缩短此代码。一种是使用IBOutletCollection. 给你的视图控制器一个实例变量,它是一个集合类型,如NSArray,并将其注释为IBOutletCollectionof UIButton

@implementation MyViewController {
    IBOutletCollection(UIButton) NSArray *bigButtons;
}
...

如果您愿意,可以在公共接口或.m文件的类扩展中将其设为属性:

#import "MyViewController.h"

// Class extension at the top of MyViewController.m
@interface MyViewController ()
@property (nonatomic, strong) IBOutletCollection(UIButton) NSArray *bigButtons;
@end

// Normal class @implementation comes after the class extension
@implementation MyViewController
...

现在,在 Interface Builder 中,您可以将此插座集合连接到所有大按钮。然后你可以在你的代码中迭代集合:

NSString * sImageName = [NSString stringWithFormat:@"button_bg_%.0f_%.0f_normal.png", L_CELL_W, L_CELL_H];
UIImage *normalImage = [UIImage imageNamed:sImageName];
sImageName = [NSString stringWithFormat:@"button_bg_%.0f_%.0f_down.png", L_CELL_W, L_CELL_H];
UIImage *highlightedImage = [UIImage imageNamed:sImageName];

for (UIButton *button in bigButtons) {
    [button setImage:normalImage forState:UIControlStateNormal];
    [button setImage:highlightedImage forState:UIControlStateHighlighted];
}

对每种类型的按钮重复(使用单独的集合 ivar)。

于 2012-11-24T07:31:22.300 回答