gpt4 book ai didi

ios - 从 UITextfield 读入 float

转载 作者:行者123 更新时间:2023-11-29 00:42:51 25 4
gpt4 key购买 nike

我一直在为 iOS 开发一个小的货币转换器,想从 UITextField 中读取一个 float 。不幸的是,我无法从 UITextField 中读取 float 。我在 Internet 上做了一些研究,但我尝试过的方法都没有奏效。我是 Objective-C 的新手,这是我自己的第一篇文章,所以请随时给我反馈和建议,以供以后发表。

这是 ViewController.m 中的代码:

//
// ViewController.m
// Currencyz
//
// Created by Daniel Putzer on 21/08/16.
// Copyright © 2016 Daniel Putzer. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize Dollar, Euro, DollarValue, EuroValue, Result, ReadIn;

- (void)viewDidLoad {
[super viewDidLoad];

[self setDollar: 0];
[self setResult: @""];
}

- (IBAction) ReadEuro:(id)sender
{
Euro = [EuroValue.text floatValue];
}

- (void) Calculate
{
Dollar = 1.13245 * Euro;
Result = [NSString stringWithFormat: @"%.2f", Dollar];
}

- (IBAction)CalculateDollar:(id)sender
{
[self Calculate];
[DollarValue setText: Result];
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

这是 ViewController.h:

//
// ViewController.h
// Currencyz
//
// Created by Daniel Putzer on 21/08/16.
// Copyright © 2016 Daniel Putzer. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
{
float Euro;
float Dollar;
NSString *Result;
NSString *ReadIn;

IBOutlet UILabel *DollarValue;
IBOutlet UITextField *EuroValue;
}

@property float Euro;
@property float Dollar;
@property NSString *Result;
@property NSString *ReadIn;

@property IBOutlet UILabel *DollarValue;
@property IBOutlet UITextField *EuroValue;

@end

我不明白为什么它不将变量 Euro 设置为在 UITextField 中输入的金额。

最佳答案

- (void)viewDidLoad {

[super viewDidLoad];

[self setDollar: 0];
[self setResult: @""];
[self ReadEuro];
}

- (void) ReadEuro
{
Euro = [EuroValue.text floatValue];
NSLog("Euro value is : %0.02f", Euro);
}

试试这个,看看是否打印出你的值。

关于ios - 从 UITextfield 读入 float,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39063052/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com