我是开发新手,有时我会编写一些非常奇怪的代码。例如,在这种情况下,我制作了一个滚动视图,用于显示从 1 到 40 的水平数字列表。每个数字都是一个按钮。我不希望当用户点击一个数字时,这个按钮的背景图像会改变,当他点击另一个时,新按钮的背景也会改变(但第一个按钮会回到他的初始状态)。就像在标签栏上...
为此,我编写了 40 次相同的代码(每个按钮):
- (IBAction)Bouton1:(id)sender {
UIImage *buttonImageOn = [UIImage imageNamed:@"fond-footer-motif-rollover.png"];
UIImage *buttonImageOff = [UIImage imageNamed:@"fond-footer-motif.png"];
[sender setBackgroundImage:buttonImageOn forState:UIControlStateNormal];
[_button2 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button3 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button4 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button5 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button6 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button7 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button8 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button9 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button10 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button11 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button12 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button13 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button14 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button15 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button16 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button17 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button18 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button19 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button20 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button21 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button22 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button23 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button24 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button25 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button26 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button27 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button28 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button29 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button30 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button31 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button32 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button33 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button34 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button35 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button36 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button37 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button38 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button39 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button40 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
}
- (IBAction)Bouton2:(id)sender {
UIImage *buttonImageOn = [UIImage imageNamed:@"fond-footer-motif-rollover.png"];
UIImage *buttonImageOff = [UIImage imageNamed:@"fond-footer-motif.png"];
[sender setBackgroundImage:buttonImageOn forState:UIControlStateNormal];
[_button1 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
//[_button2 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button3 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button4 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button5 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button6 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button7 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button8 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button9 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button10 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button11 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button12 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button13 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button14 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button15 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button16 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button17 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button18 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button19 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button20 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button21 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button22 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button23 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button24 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button25 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button26 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button27 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button28 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button29 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button30 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button31 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button32 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button33 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button34 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button35 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button36 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button37 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button38 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button39 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
[_button40 setBackgroundImage:buttonImageOff forState:UIControlStateNormal];
}
因此,当我单击一个按钮时,它会更改所有其他按钮.. 我怎样才能编写更好的代码?