【问题标题】:IBAction to perform operation on another IBActionIBAction 对另一个 IBAction 执行操作
【发布时间】:2012-08-01 00:06:59
【问题描述】:

我是 Objective c 的新手,刚刚开始编写我的第一段代码,所以请在您的回复中温柔地对待我。

考虑到它本质上是一个简单的减法机,我决定在我第一次尝试编程时尝试做一个飞镖计算器。

在我的视图 controller.h 文件中,我有以下内容:

-(IBAction)subtractOne:(id)sender;

在 viewController.m 中

-(IBAction)subtractOne:(id)sender

{
    accumulator -= 1;
    calculatorScreen.text = [NSString stringWithFormat:@"%i", accumulator];    
}

我已经为所有 22 个计分按钮设置了这些,并设法让所有这些都正常工作。

但是我现在的困难在于双键和高音按钮。我需要一个 -(IBAction) 将 nextButtonPressed 乘以 2 或 3,但我不确定如何进行。

我并不是要求您为我编写代码,但如果您能指出我将如何执行此操作的方向,我们将不胜感激。

提前致谢。

【问题讨论】:

    标签: objective-c xcode4 calculator


    【解决方案1】:
    -(IBAction) tripleMultiplier:(id)sender;
    
    -(IBAction) tripleMultiplier:(id)sender {
        // Activate the triple multipler if it is inactive, otherwise deactivate it.
        tripleMultiplier = !tripleMultiplier;
    }
    

    在其他IBACtions

    -(IBAction) subtractOne:(id)sender {
        // If the tripleMultipler is active: 
        accumulator -= (tripleMultipler ? 1 : 3);
        calculatorScreen.text = [NSString stringWithFormat:@"%i", accumulator];    
    }
    

    【讨论】:

    • 嗨,迈克尔,感谢您的快速回复。我假设第一个 IBAction 在头文件中,第二个在主文件中,第三段代码嵌入在我已经编写的代码中。它是否正确?我已经尝试过这个并且不断收到错误说未声明的标识符tripleMultiplier!请问有什么建议吗?再次感谢。
    • @AlanBurnell 在你的头文件中你需要BOOL tripleMultiplier,然后在你的实现的init, tripleMultiplier = NO 中初始化它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-13
    相关资源
    最近更新 更多