将 UITextField 值转换为 NSString

发布于 2024-11-10 05:34:18 字数 673 浏览 3 评论 0原文

我有一个应用程序的一部分用于将单位从英尺和英寸转换为厘米。

我通过两个 UITextField 输入接受用户的英尺和英寸,效果很好。然后我想对输入的值执行计算并将结果显示在下面的标签中。点击“转换”按钮时我用于计算的代码如下;

-(IBAction)heighConvertButtonAction:(id)sender {

    NSString *feetCM = heightFeetField.text;
    NSString *inchesCM = heightInchesField.text;

    float fFeet = 30.48 * [feetCM integerValue];
    float fInches = 2.54 * [inchesCM integerValue];
    float fFeetInches = fFeet + fInches;
    fFeetInchesResult = [NSString stringWithFormat:@"%", fFeetInches];

    heightCMLabel.text = fFeetInchesResult;

的工作原理是,它不会产生任何错误,但是,当触摸转换按钮时,它不会执行任何操作。我希望它能用结果填充“heightCMLabel”标签。

任何帮助将不胜感激。

谢谢

I am having a section of an application used for converting units from feet and inches to centimetres.

I accept the feet and inches from the user through two UITextField inputs which works fine. I then want to perform a calculation on the inputted values and display the result in a Label underneath. The code I am using for the calc when the 'convert' button is tapped is as follows;

-(IBAction)heighConvertButtonAction:(id)sender {

    NSString *feetCM = heightFeetField.text;
    NSString *inchesCM = heightInchesField.text;

    float fFeet = 30.48 * [feetCM integerValue];
    float fInches = 2.54 * [inchesCM integerValue];
    float fFeetInches = fFeet + fInches;
    fFeetInchesResult = [NSString stringWithFormat:@"%", fFeetInches];

    heightCMLabel.text = fFeetInchesResult;

}

It works in the sense that it doesnt produce any errors, however, when the convert button is touched it doesn't do anything. I was hoping it would populate the 'heightCMLabel' label with the result.

Any help would be appreciated.

Thanks

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

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

发布评论

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

评论(1

携余温的黄昏 2024-11-17 05:34:18
fFeetInchesResult = [NSString stringWithFormat:@"%", fFeetInches];

不确定这是否只是发布代码时的错误,但这不是有效的字符串格式化程序。

尝试:

fFeetInchesResult = [NSString stringWithFormat:@"%f", fFeetInches];
fFeetInchesResult = [NSString stringWithFormat:@"%", fFeetInches];

Not sure if it was just an error when posting your code, but that's not a valid string formatter.

Try:

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