按下按钮时如何锁定动作?

时间:2014-03-26 12:49:06

标签: ios iphone objective-c

我正在制作一个文字游戏,有三个提示,每个花费10个硬币,让我们说玩家只有8个硬币,然后他不能使用第2和第3个提示,

因此,当点击提示按钮时,我将如何阻止操作:这样它不会显示提示或者它不会更改为其他按钮图像

代码:

- (IBAction)secondHintq:(id)sender {
// - Here image is changing when button is clicked - this has to be locked
[_candletwo setImage:[UIImage imageNamed:@"candle2_03.png"] forState:UIControlStateNormal];

// This is the text that is displayed when clicked - this has to be locked
hintView.text = @"Type in text here 2";
if(!btn2Pressed) {
    if((coins -10) >= 0){
        coins = coins -10;
        score = score -2;
        btn2Pressed = true;
        coinsLabel.text = [NSString stringWithFormat:@"%d",coins];
    }
    else{
        //Show an alert that the user has not enough coins
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"TITLE" message:@"MESSAGE" delegate:nil cancelButtonTitle:@"RETURN BUTTON" otherButtonTitles:nil];
        [alert show];

    }
   }

这是怎么做到的?

5 个答案:

答案 0 :(得分:1)

可能会移动您的代码吗?

if((coins -10) >= 0){
[_candletwo setImage:[UIImage imageNamed:@"candle2_03.png"] forState:UIControlStateNormal];

// This is the text that is displayed when clicked - this has to be locked
hintView.text = @"Type in text here 2";
}

答案 1 :(得分:0)

Remove target action button excute [yourBtn removeTarget:self action:@selector(secondHintq:) forControlEvents:UIControlEventTouchUpInside] 时不需要[yourBtn addTarget:self action:@selector(secondHintq:) forControlEvents:UIControlEventTouchUpInside] 这样:

userInteractionEnabled property

在需要时添加目标,如下所示:

yourBtn.userInteractionEnabled = NO;

编辑:最佳选项使用{{1}}。这些禁止在按钮上进行任何选择

{{1}}

答案 2 :(得分:0)

试试这个,

button.userInteractionEnabled = NO;

答案 3 :(得分:0)

//Check for coints validity in any button tap event

- (BOOL)checkValidCoins {
    if(coins>=10) {
        return YES;
    }
    return NO;
}

- (IBAction)secondHintq:(id)sender {
    UIButton *btn = (UIButton *)sender;
    if([self checkValidCoins] && !btn.selected) {
        hintView.text = @"Type in text here 2";
        [_candletwo setImage:[UIImage imageNamed:@"candle2_03.png"] forState:UIControlStateNormal];
        coins -= 10;
        score -= 2;
        btn.selected = YES;
        coinsLabel.text = [NSString stringWithFormat:@"%d",coins];
    }else{
        if(btn.selected) {
            // Show an alert that the user has not enough coins
        }else{
            // Hint already visible
        }
    }
}

答案 4 :(得分:0)

或者,您可以像这样切换button.enabled属性:

button.enabled = NO
相关问题