xcode 如何为用户键入输入编码并在公式中使用它?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6585490/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to code for user typing input and use that in a formula?
提问by Kylie Moden
I'm a student using Xcode 3.2 and What I need to know is how the user can type input and then I can use that input in a formula. I.e. the user inputs 5 and in another field 7 and then I use both numbers in a math formula to return a value
我是一名使用 Xcode 3.2 的学生,我需要知道的是用户如何输入输入,然后我可以在公式中使用该输入。即用户输入 5 和另一个字段 7 然后我在数学公式中使用这两个数字来返回一个值
I'm new to Cand C++ (I use java), and I have been searching the web for an answer and I somehow can't find what I'm looking for although it's a relatively simple concept. Code examples are definitely preferred and greatly appreciated.
我是C和 C++ 的新手(我使用 java),我一直在网上搜索答案,尽管这是一个相对简单的概念,但我以某种方式找不到我要找的东西。代码示例绝对是首选,非常感谢。
回答by MacN00b
Ok the best way to do this is to convert the numbers inputed into floats then do whatever math problem you want and then convert the answer into a string and display the string in a label. As you can see here in my .h file:
好的,最好的方法是将输入的数字转换为浮点数,然后执行您想要的任何数学问题,然后将答案转换为字符串并在标签中显示该字符串。正如您在我的 .h 文件中看到的:
IBOutlet UITextField *rectWidth;
IBOutlet UITextField *rectLength;
IBOutlet UILabel *rectResult;
}
@property (retain, nonatomic) UITextField *rectWidth;
@property (retain, nonatomic) UITextField *rectLength;
@property (retain, nonatomic) UILabel *rectResult;
-(IBAction)calculate:(id)sender;
As you can see I have 2 text fields for the user to input the data. Right now I was using it for an area and volume finder but you can name them whatever you want. Also I have the calculate action to tell the program what to do when the button is clicked. Then in your .m
如您所见,我有 2 个文本字段供用户输入数据。现在我将它用于区域和体积查找器,但您可以随意命名它们。我也有计算操作来告诉程序在单击按钮时要做什么。然后在你的 .m
-(IBAction)calculate:(id)sender {
float floatRectResult=[rectWidth.text floatValue]*
[rectLength.text floatValue];
NSString *stringRectResult=[[NSString alloc]
initWithFormat:@"%1.2f",floatRectResult];
rectResult.text=stringRectResult;
[stringRectResult release];
}
So all you need to do is put this code in and change the * behind float floatRectResult=[rectWidth.text floatValue] to whatever formula you want to use. Like addition, subtraction division etc. So in interface builder connect the text fields to the two text fields and the label to the label on screen. Then create a button and link it to the IBAction calculate. And that should be all. Hope this helps.
所以你需要做的就是把这段代码放进去,然后把 float floatRectResult=[rectWidth.text floatValue] 后面的 * 改成你想使用的任何公式。像加法,减法等。所以在界面构建器中,将文本字段连接到两个文本字段,并将标签连接到屏幕上的标签。然后创建一个按钮并将其链接到 IBAction 计算。这应该是全部。希望这可以帮助。