我正在开发一个计算器应用程序,我试图通过更改背景颜色突出显示触摸的按钮。我的问题是,当我第一次触摸按钮时,它会显示数字,但背景颜色不会改变。但后来它工作正常,只是第一次触摸根本不起作用。我感谢任何帮助!
#import "ViewController.h"
#import <QuartzCore/QuartzCore.h>
#import <AudioToolbox/AudioToolbox.h>
@interface ViewController ()
@end
@implementation ViewController
@synthesize resultLabel;
BOOL isDecimal;
NSArray *array;
NSNumber *number;
NSNumberFormatter *formatter;
- (void)viewDidLoad
{
[super viewDidLoad];
array = [[NSArray alloc]initWithObjects:ACbuttonOutlet, deleteButtonOutlet, plusMinusOutlet, divideOutlet, number7Outlet, number8Outlet, number9Outlet, multiplicationOutlet, number4Outlet, number5Outlet, number6Outlet, plusOutlet, number1Outlet, number2Outlet, number3Outlet, minusOutlet, number0Outlet, decimalOutlet, resultOutlet, nil];
for(UIButton *button in array)
{
[button.layer setBorderWidth:0.25];
[button.layer setBorderColor:[UIColor blackColor].CGColor];
}
resultLabel.adjustsFontSizeToFitWidth = YES;
/*formatter = [[NSNumberFormatter alloc]init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter setGroupingSize:3];
[formatter setGroupingSeparator:@" "];
number = [NSNumber numberWithInt:[resultLabel.text intValue]];
resultLabel.text = [formatter stringFromNumber:number];*/
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)highlightButton:(UIButton *)button
{
AudioServicesPlaySystemSound(0x450);
[button setBackgroundColor:[UIColor colorWithRed:239/255.0 green:232/255.0 blue:232/255.0 alpha:1]];
NSLog(@"1");
}
- (void)resetButtonBackGroundColor: (UIButton *)sender{
[sender setBackgroundColor:[UIColor whiteColor]];
NSLog(@"2");
}
- (IBAction)ACbutton:(id)sender {
[sender addTarget:self action:@selector(highlightButton:) forControlEvents:UIControlEventTouchDown];
[sender addTarget:self action:@selector(resetButtonBackGroundColor:) forControlEvents:UIControlEventTouchUpInside];
isDecimal = NO;
Method = 0;
SelectNumber = 0;
RunningTotal = 0;
resultLabel.text = [NSString stringWithFormat:@"0"];
}
答案 0 :(得分:0)
尝试将viewDidLoad方法中的这一行添加到循环中
for(UIButton *button in array)
{
[button.layer setBorderWidth:0.25];
[button.layer setBorderColor:[UIColor blackColor].CGColor];
[button addTarget:self action:@selector(highlightButton:) forControlEvents:UIControlEventTouchDown];
}
答案 1 :(得分:0)
我建议你不要使用按钮的背景颜色。而是切换/更改按钮的背景图像。