开发者

iPhone disable button that was tapped

开发者 https://www.devze.com 2023-01-22 22:50 出处:网络
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, o

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开发者_如何学Python help. Thank you!


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];
}


[self.yourButton addTarget:self action:@selector(yourAction) forControlEvents:UIControlEventTouchUpInside];

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


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.
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号