禁用IBAction链接按钮

时间:2011-07-14 21:43:30

标签: objective-c button ibaction

我使用Interface Builder创建了一个按钮并将其链接到一个动作。我想在下面的if声明中停用匹配按钮。

- (IBAction)hit:(id)sender {
    Application *app = [[Application alloc] init];
    int nc = [app dealCard];
    [userOne setIntValue:tu];
    [userTwo setIntValue:nc];
    tu += nc;
    [totalUser setIntValue:tu];
    BOOL bust = [app checkBust:tu];
    if (bust == YES) {
        [console setIntValue:1];
        //Disable button here.
    }
}

我该怎么办?

2 个答案:

答案 0 :(得分:1)

UIButtonUIResponder的子类,具有enabled属性。将其设置为NO以禁用按钮中的操作。 E.g。

UIButton *theButton = (UIButton *)sender;
theButton.enabled = NO;

答案 1 :(得分:1)

我发现了问题。事实证明我使用的是NSButton而不是UIButton,所以我将声明更改为:NSButton *theButton = (NSButton *)sender;

然后我将theButton.enabled = NO;替换为[theButton setEnabled = NO];

所以这是我完成的代码:

- (IBAction)hit:(id)sender {
    Application *app = [[Application alloc] init];
    NSButton *theButton = (NSButton *)sender;
    int nc = [app dealCard];
    [userOne setIntValue:tu];
    [userTwo setIntValue:nc];
    tu += nc;
    [totalUser setIntValue:tu];
    BOOL bust = [app checkBust:tu];
    if (bust == YES) {
        [console setIntValue:1];
        [theButton setEnabled = NO];
    }
}