使用 Xcode 通过 (id)sender 标签实现十进制
我有一个简单的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
下面的代码应该正确更新屏幕。
要在计算中使用它,请使用
The code below should update the screen correctly.
To use this in calculations, use