Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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?

share|improve this question

closed as off-topic by nhgrif, Simon André Forsberg, Kid Diamond, 200_success Feb 18 at 9:05

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

1  
This question looks a bit generic and hypothetical. If it your actual code? If so, please provide some more information: _why do you have 17 buttons, and are there any relations among them? –  200_success Feb 17 at 7:29
1  
I agree with @200_success. Let's see some more context. Why do you have 17 buttons on the screen versus say a table view or a collection view? Or why have buttons at all versus just a single view that calculates where the user touched? What are you trying to accomplish? –  nhgrif Feb 17 at 22:30
    
As written, this question is essentially a duplicate of this: codereview.stackexchange.com/questions/41761/… –  nhgrif Feb 17 at 22:31

1 Answer 1

You could add tags to each button, and they could all respond to the same action

NSInteger maxCount = 17;
for( int i = 0; i < maxCount; i++ ){
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [btn setTitle:[NSString stringWithFormat:@"Button%d", i] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [btn setTag:i];
    [self.view addSubview:btn];
}

Then you get your UIButton by casting the sender parameter in your action

- (IBAction)buttonPressed:(id)sender{
     UIButton *btn = (UIButton *)sender;
     [self touchedAtPoint:btn onPosition:[btn tag]];
     btn.userInteractionEnabled = NO;
}
share|improve this answer
    
This answer still contains some duplication... I'm not to familiar with Objective C, but would it be possible to wrap button creation into a separate function too? –  Pimgd Feb 17 at 8:50
    
@Pimgd - you can by using a loop. I edited the code above, although I'm quite sure it's not a good approach since you still need to add them to the view (that is if you're adding them programmatically. I'm not sure if there is another way but if you're using a storyboard/XIB you have to do them manually. The asker didn't state how his/her buttons were created). What you can do then is add them to an array and use objectAtIndex: to get the right UIButton. –  randomProgrammer Feb 17 at 9:50

Not the answer you're looking for? Browse other questions tagged or ask your own question.