在一个 iPhone 应用程序中,我在一个视图中有一堆 UIButtons(比如大约 60 个按钮)。我正在使用以下方法来自定义按钮的外观。
- (void) setButtonAttributes:(UIButton *) buttonName withTitle:(NSString *)title withImage:(NSString *) imageSuffix
{
NSString *unpressedName, *pressedName;
pressedName = [NSString stringWithFormat:@"pressed%@.png", imageSuffix];
unpressedName = [@"un" stringByAppendingString: pressedName];
buttonName.layer.cornerRadius = 5;
buttonName.clipsToBounds = YES;
[buttonName setTitle:title forState:UIControlStateNormal];
if ([imageSuffix isEqualToString:@""] || [imageSuffix isEqualToString:@"brown"] || [imageSuffix isEqualToString:@"green"])
{
buttonName.titleLabel.font = [UIFont boldSystemFontOfSize:15];
[buttonName setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[buttonName setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
[buttonName setBackgroundImage:[UIImage imageNamed:unpressedName] forState:UIControlStateNormal];
[buttonName setBackgroundImage:[UIImage imageNamed:pressedName] forState:UIControlStateHighlighted];
}
else
{
[buttonName setImage:[UIImage imageNamed:unpressedName] forState:UIControlStateNormal];
[buttonName setImage:[UIImage imageNamed:pressedName] forState:UIControlStateHighlighted];
}
[buttonName addTarget:self action:@selector(playClick) forControlEvents:UIControlEventTouchDown];
}
因此,我在视图的 - (void)viewDidLoad 中调用了这个方法大约 60 次。例如:
[self setButtonAttributes:firstButton withTitle:@"First" withImage:@"brown"];
一些按钮在 IB 中可见。但也有一些按钮在 IB 中不可见,所以我使用以下内容来展示它们:
secondButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
secondButton.frame = CGRectMake(64, 394, BUTTOM_WIDTH, BUTTOM_HEIGHT);
secondButton.bounds = CGRectMake(0, 0, BUTTOM_WIDTH, BUTTOM_HEIGHT);
[self setButtonAttributes:secondButton withTitle:@"Second" withImage:@"brown"];
[secondButton addTarget:self action:@selector(someSelector) forControlEvents:UIControlEventTouchUpInside];
[somePane addSubview:secondButton]; //somePane is a scrollview.
并且所有这 60 个按钮都在视图的头文件中定义了一个 IBOutlet。例如:
IBOutlet UIButton *firstButton;
当我将我的应用程序与 iPhone 的计算器应用程序进行比较时,我注意到 iPhone 的计算器中的按钮感觉比我的应用程序快得多。所以基本上,我的问题是与 iPhone 的计算器相比,我的应用程序中按钮的响应时间要慢得多。我想知道这是否与我在上面的代码中解释的自定义按钮的方式有关?如果是这样的话,自定义这么多按钮的正确方法是什么?