I have an iOS app where I use some buttons for different things, but I'm repeating a lot.
For buttons pressed:
- (IBAction)button_1_pressed:(id)sender {
[self touchedAtPoint:sender onPosition:1];
self.button_1.userInteractionEnabled = NO;
}
- (IBAction)button_2_pressed:(id)sender {
[self touchedAtPoint:sender onPosition:2];
self.button_2.userInteractionEnabled = NO;
}
- (IBAction)button_3_pressed:(id)sender {
[self touchedAtPoint:sender onPosition:3];
self.button_3.userInteractionEnabled = NO;
}
- (IBAction)button_4_pressed:(id)sender {
[self touchedAtPoint:sender onPosition:4];
self.button_4.userInteractionEnabled = NO;
}
For some behavior I called programatically buttonPressed
:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[self enableAllButtons];
UITouch *touched = [[event allTouches] anyObject];
//know when touch on top of view
CGPoint locationRelative = [touched locationInView:self.button_1];
if (CGRectContainsPoint(self.button_1.bounds, locationRelative)) {
[self button_1_pressed:self.button_1];
}
locationRelative = [touched locationInView:self.button_2];
if (CGRectContainsPoint(self.button_2.bounds, locationRelative)) {
[self button_2_pressed:self.button_2];
}
locationRelative = [touched locationInView:self.button_3];
if (CGRectContainsPoint(self.button_3.bounds, locationRelative)) {
[self button_3_pressed:self.button_3];
}
}
I have 17 buttons. How can I clean this up?