使用 Xcode 通过 (id)sender 标签实现十进制

发布于 2024-11-17 02:52:19 字数 2546 浏览 2 评论 0原文

我有一个简单的 iOS/objective C 问题。我搜索了论坛并尝试了几种不同的方法,但我似乎无法弄清楚这一点,我对编码非常陌生,所以我对此表示歉意,这可能看起来很愚蠢。我正在尝试构建一个带有从头开始制作的按钮的简单计算器,但我卡在了小数部分。我正在使用 switch 语句通过按钮 (id)sender 输入我的号码。你可以看到我注释掉的部分,这是我要获取标记为 10 的 id 并编写某种语句以将小数实现为浮点值的地方。

我还看了之前的一篇文章,其中附加了小数位。我尝试更改代码,但似乎我在这里遗漏了很多东西。

-(IBAction)decimalButton:(id)sender{
    calculatorScreen.text = [calculatorScreen.text stringByAppendingFormat:@"."]; 
}

任何帮助/建议/示例代码都会非常有帮助,我再次对新手问题感到抱歉,我正在尽力学习基础知识。

#import "calcViewController.h"

@implementation calcViewController



-(IBAction)buttonDigitPressed:(id)sender { //ditits presed with sender tag
//    switch ([sender tag]) {
//        case 1:
//            currentNumber = 1;
//            break;
//        case 2:
//            currentNumber = 2;
//            break;
//        case 3:
//            currentNumber = 3;
//            break;
//        case 4:
//            currentNumber = 4;
//            break;
//        case 5:
//            currentNumber = 5;
//            break;
//        case 6:
//            currentNumber = 6;
//            break;
//        case 7:
//            currentNumber = 7;
//            break;
//        case 8:
//            currentNumber = 8;
//            break;
//        case 9:
//            currentNumber = 9;
//            break;
//        case 10:
//            //possibly implement decimal here 
//            break;
//    }


    currentNumber = (float)[sender tag]; //sets currentNumber to buttonPressed
    calculatorScreen.text = [NSString stringWithFormat:@"%g",currentNumber];//displays button
}


-(IBAction)buttonOperationPressed:(id)sender {
    //currentOperation is 0 by system default?

    if (currentOperation == 0) 
        result = currentNumber;

    else {
        switch (currentOperation) {
            case 1:
                result = result + currentNumber;
                break;
            case 2:
                result = result - currentNumber;
                break;
            case 3:
                result = result * currentNumber;
                break;
            case 4:
                result = result / currentNumber;
                break;
            case 5:
                currentOperation = 0;
                break;
        }
    }
    //currentNumber = 0;

    currentNumber = result; //current number equals last resulting equation
    calculatorScreen.text = [NSString stringWithFormat:@"%g",result]; //screen prints result
    if ([sender tag] == 0) result = 0;
    currentOperation = [sender tag];
}

I have a simple iOS/objective C question. I have searched the forum and have tried a couple different approaches but I cant seem to figure this out, I am very new to coding so I apologize for this may seem silly. I am trying to build a simple calculator with buttons made from scratch but I am stuck on the decimal portion. I am using a switch statement to input my numbers through the button (id)sender. You can see the portion I commented out, this is where I was going to take the id tagged with 10 and write some kind of statement to implement a decimal into the float value.

I also looked at a previous post which appends the decimal place. I tried to alter the code but it seems like im missing quite a few things here.

-(IBAction)decimalButton:(id)sender{
    calculatorScreen.text = [calculatorScreen.text stringByAppendingFormat:@"."]; 
}

Any help/suggestions/sample code would be very helpful, again im sorry for the newbie question, im trying my best to learn the basics.

#import "calcViewController.h"

@implementation calcViewController



-(IBAction)buttonDigitPressed:(id)sender { //ditits presed with sender tag
//    switch ([sender tag]) {
//        case 1:
//            currentNumber = 1;
//            break;
//        case 2:
//            currentNumber = 2;
//            break;
//        case 3:
//            currentNumber = 3;
//            break;
//        case 4:
//            currentNumber = 4;
//            break;
//        case 5:
//            currentNumber = 5;
//            break;
//        case 6:
//            currentNumber = 6;
//            break;
//        case 7:
//            currentNumber = 7;
//            break;
//        case 8:
//            currentNumber = 8;
//            break;
//        case 9:
//            currentNumber = 9;
//            break;
//        case 10:
//            //possibly implement decimal here 
//            break;
//    }


    currentNumber = (float)[sender tag]; //sets currentNumber to buttonPressed
    calculatorScreen.text = [NSString stringWithFormat:@"%g",currentNumber];//displays button
}


-(IBAction)buttonOperationPressed:(id)sender {
    //currentOperation is 0 by system default?

    if (currentOperation == 0) 
        result = currentNumber;

    else {
        switch (currentOperation) {
            case 1:
                result = result + currentNumber;
                break;
            case 2:
                result = result - currentNumber;
                break;
            case 3:
                result = result * currentNumber;
                break;
            case 4:
                result = result / currentNumber;
                break;
            case 5:
                currentOperation = 0;
                break;
        }
    }
    //currentNumber = 0;

    currentNumber = result; //current number equals last resulting equation
    calculatorScreen.text = [NSString stringWithFormat:@"%g",result]; //screen prints result
    if ([sender tag] == 0) result = 0;
    currentOperation = [sender tag];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我不是你的备胎 2024-11-24 02:52:19

下面的代码应该正确更新屏幕。

-(IBAction)buttonDigitPressed:(id)sender { //ditits presed with sender tag
    switch ([sender tag]) {
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
        case 6:
        case 7:
        case 8:
        case 9:
            calculatorScreen.text = [calculatorScreen.text stringByAppendingFormat:@"%d", [sender tag]];
            break;

        case 10:
            NSRange range = [calculatorScreen.text rangeOfString:@"."];
            if ( range.location == NSNotFound ) {
                calculatorScreen.text = [calculatorScreen.text stringByAppendingString:@"."];
            }
            break;
    }
}

要在计算中使用它,请使用

float screenNumber = [calculatorScreen.text floatValue];

The code below should update the screen correctly.

-(IBAction)buttonDigitPressed:(id)sender { //ditits presed with sender tag
    switch ([sender tag]) {
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
        case 6:
        case 7:
        case 8:
        case 9:
            calculatorScreen.text = [calculatorScreen.text stringByAppendingFormat:@"%d", [sender tag]];
            break;

        case 10:
            NSRange range = [calculatorScreen.text rangeOfString:@"."];
            if ( range.location == NSNotFound ) {
                calculatorScreen.text = [calculatorScreen.text stringByAppendingString:@"."];
            }
            break;
    }
}

To use this in calculations, use

float screenNumber = [calculatorScreen.text floatValue];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文