0

Alright guys, I'm sure there is a simple solution to this problem. I have four IBActions;up,down,left,right. I only want One action to execute leaving the other three available, e.g. If I press up, only down,left and right are available to be pressed, so I can't press the same action twice in a row. And it's the same for the other actions, e.g. I can only press left once then must press an action for right,down, or up. If anyone knows the solution to this I would appreciate the help. Thank you!

3 Answers 3

6

Keep an array of the buttons, then on your various handlers you'll set them all enabled except the one that was tapped. For example:

- (IBAction)up {
    for(UIButton *button in self.allButtons) {
        [button setEnabled:YES];
    }

    [self.upButton setEnabled = NO];
}
0
1
[self.yourButton addTarget:self action:@selector(yourAction) forControlEvents:UIControlEventTouchUpInside];

- (void)yourAction
{
    [self.yourButton setEnabled:NO];
}
0

I like the idea that Ben Scheiman has in his answer about having the array of buttons but instead of having a method for each of the buttons it would make more sense to just have one method that does it all for whichever button you press. Also I'm not 100% sure that last line of his code is correct. So a cleaner way to do it would be to do:

// Probably the biggest difference is I have added a parameter that will be the button. 
- (IBAction)directionalButtonLock:(id)sender 
{
    // Credit to Ben Scheiman's answer here.
    for (UIButton *button in [self allButtons]) {
        [button setEnabled:YES];
    }
    [sender setEnabled:NO]; // Disable the pressed button.
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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