计算值不会显示在文本字段中

时间:2014-01-15 10:08:06

标签: objective-c textfield calculator

我在xcode中正在做一个应用程序。它旨在进行计算,将滑块值除以100乘以由用户确定的文本字段(numberfield1)值。假设结果显示在另一个文本字段(numberfield2)中。但是,该值不会显示在第二个文本字段中。我的问题是我需要在代码中更正以在文本字段中显示计算结果?

这是我的.h文件:

@property (weak, nonatomic) IBOutlet UISlider *slider;
@property (weak, nonatomic) IBOutlet UILabel *counter;
@property (weak, nonatomic) IBOutlet UILabel *label;
@property (weak, nonatomic) IBOutlet UITextField *numberfield1;
@property (weak, nonatomic) IBOutlet UITextField *numberfield2;

- (IBAction)changeLabel;
- (IBAction)calculate:(id)sender;

继承我的.m文件:

- (IBAction)changeLabel {_counter.text = [[NSString alloc] initWithFormat:@"%1.f"       
,_slider.value];}


- (IBAction)calculate:(id)sender {
float x = [_counter.text floatValue];
float y = 100;
float calc_result = x / y;
float numberField1Float = [_numberfield1.text floatValue]; //converts the string in the text field to a float
float  calc_result2 = calc_result * numberField1Float;
self.numberfield2.text = [NSString stringWithFormat:@"%0.1f", calc_result2];
}

1 个答案:

答案 0 :(得分:0)

试试这个:

[self.numberfield2 setStringValue: 
    [NSString stringWithFormat: @"%0.1f", calc_result2]];